-
Notifications
You must be signed in to change notification settings - Fork 424
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
Comments
It is possible to get the original arguments from within the application via the 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? |
Actually what I was struggling was, I assign the default value to some options, by assigning a value to it when defining it.
Then I found there is no way I can get it. |
You can use
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
|
I added a new section to the user manual for this: Can you review and provide feedback? Thanks! |
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. |
Great, glad to hear that! Don’t forget to star ⭐️ picocli on GitHub if you like the project! 😉 |
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
The text was updated successfully, but these errors were encountered: