question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom handling of positional parameters

See original GitHub issue

I would like to support some rich syntax with my CLI tool. I would like to allow the user to not only pass in a list of integers as positional arguments, but ranges of integers. I also don’t want to limit them on how these parameters are mixed together with the others.

Example:

foocli 1 2 4 6 8

Passes in the integer array [1,2,4,6,8] as positional arguments

foocli 1 3-5 2, 8

Passes in the integer array [1, 3, 4 5, 2, 8] as positional arguments.

I would ultimately like to support any range of numbers in any positional argument.

I know this is a rather special-purpose feature and that I can implement it myself using a list of string positional parameters, which I convert to integers myself. Any ideas on how to support it using a mix of picocli features and custom code would be nice.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
remkopcommented, Aug 17, 2018

I’m thinking the @Unmatched idea was also a workaround to begin with. What you really want is to be able to parse input that is a mixture of simple scalars and range expressions. I had another idea: why not use a custom converter?

class RangeConverter implements ITypeConverter<Set<Integer>> {
    // convert each argument to a set of integers;
    // first parse as scalar, if that fails, parse as range
}

Declare the field as a set of sets:

    @CommandLine.Parameters(arity="1..*")
    Set<Set<Integer>> viewIds;

And flatten the set in the application:

Set<Object> flat = 
    Set.stream()
        .flatMap(Set::stream)
        .collect(Collectors.toSet());
1reaction
remkopcommented, Aug 17, 2018

Let me take a look…

Read more comments on GitHub >

github_iconTop Results From Across the Web

About positional parameters - Product documentation
The positional parameters are interspersed with nonpositional parameters in the volume create command, and the positional parameter values are ...
Read more >
Overcoming Positional Parameter Parsing in Java - DZone
This primer into positional parameter parsing using a custom class is a good reminder of how to keep your programs flexible. Bipin Patwardhan ......
Read more >
Ways to set positional parameters in bash
-- If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters are set to the arguments, ...
Read more >
Is mixing getopts with positional parameters possible?
Sorry if it is not clear from question. Yes. I want to enable my script to handle a mix of positional parameters and...
Read more >
Adding arguments and options to your Bash scripts - Red Hat
The ability to use positional parameters—otherwise known as arguments—to specify data to be used as values for variables in the scripts is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found