Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output all args #1160

Closed
jasonlu-pillar opened this issue Aug 28, 2020 · 6 comments
Closed

Output all args #1160

jasonlu-pillar opened this issue Aug 28, 2020 · 6 comments

Comments

@jasonlu-pillar
Copy link

Is there a way to output all the args with value ? Sorry if this is a naive qustion.
args maybe passed in or not.
Either build it into a string or output stream

@remkop
Copy link
Owner

remkop commented Aug 28, 2020

It is possible to get the original arguments from within the application via the ParseResult.

Example:

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.ParseResult;
import picocli.CommandLine.Spec;

import java.util.List;

@Command(name = "myapp", mixinStandardHelpOptions = true, version = "myapp 0.1")
public class ParseResultDemo implements Runnable {
    @Spec CommandSpec spec;

    @Option(names = "-x")
    int x;

    @Parameters
    List<String> positionalParams;

    @Override
    public void run() {
        ParseResult parseResult = spec.commandLine().getParseResult();

        System.out.println(parseResult.expandedArgs()); // all args after expanding any @file arguments
        System.out.println(parseResult.originalArgs()); // original args prior to expanding @file arguments
    }

    public static void main(String[] args) {
        new CommandLine(new ParseResultDemo()).execute(args);
    }
}

Does this answer your question?

@jasonlu-pillar
Copy link
Author

Actually what I was struggling was, I assign the default value to some options, by assigning a value to it when defining it.

@Option(names = "-x")
int x  = 10;

Then I found there is no way I can get it.
How about those argument annotated with defaultValue?

@remkop
Copy link
Owner

remkop commented Aug 29, 2020

You can use ParseResult::matchedOptionValue to get the value that was actually matched on the command line.

OptionSpec::getValue will return the value assigned (which may be a command line arg or may be the default value).

I updated the example. You can use code like this to print values for all options:

@Option(names = "-x")
int x = 10;

@Option(names = "-y", defaultValue = "20")
int y;

@Override
public void run() {
    ParseResult pr = spec.commandLine().getParseResult();

    System.out.println(pr.expandedArgs());
    System.out.println(pr.originalArgs());

    List<OptionSpec> options = spec.options();
    for (OptionSpec opt : options) {
        System.out.printf("%s was specified: %s%n",
                opt.longestName(), pr.hasMatchedOption(opt));
        System.out.printf("%s=%s (-1 means this option was not matched on command line)%n",
                opt.shortestName(), pr.matchedOptionValue(opt.longestName(), -1));
        System.out.printf("%s=%s (arg value or default)%n",
                opt.longestName(), opt.getValue());
        System.out.println();
    }

    System.out.printf("-x=%s (field value)%n", x);
    System.out.printf("-y=%s (field value)%n", y);
}

For input -x 3, I get this output:

[-x, 3]
[-x, 3]
-x was specified: true
-x=3 (-1 means this option was not matched on command line)
-x=3 (arg value or default)

-y was specified: false
-y=-1 (-1 means this option was not matched on command line)
-y=20 (arg value or default)

--help was specified: false
-h=-1 (-1 means this option was not matched on command line)
--help=false (arg value or default)

--version was specified: false
-V=-1 (-1 means this option was not matched on command line)
--version=false (arg value or default)

-x=3 (field value)
-y=20 (field value)

@remkop
Copy link
Owner

remkop commented Aug 30, 2020

I added a new section to the user manual for this:
https://picocli.info/#_was_a_value_defaulted

Can you review and provide feedback? Thanks!

@jasonlu-pillar
Copy link
Author

Thanks for the tip!It really helped!

The change to the user manual looks good, making it more clear for user to use this feature.

@remkop
Copy link
Owner

remkop commented Aug 30, 2020

Great, glad to hear that!
I’ll close this issue, feel free to open another if you have more questions.

Don’t forget to star ⭐️ picocli on GitHub if you like the project! 😉

@remkop remkop closed this as completed Aug 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants