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

Programmatically "fixing" parameters after parsing #55

Closed
martinpaljak opened this issue Feb 10, 2014 · 8 comments
Closed

Programmatically "fixing" parameters after parsing #55

martinpaljak opened this issue Feb 10, 2014 · 8 comments

Comments

@martinpaljak
Copy link
Contributor

Maybe it is obvious (or a dumb idea) but going through the examples and docs I did not find a way to "fix up" parameters.

If a parameter -X (input source) is given, some other parameters -Y and -Z can be set to a default value read from that source and the rest of the code could work just the way it is, making decisions based on the presence and values of Y and Z. So I'd like to write something along the lines of

if args.has(X): args.Z = X.Z
@pholser
Copy link
Collaborator

pholser commented Aug 1, 2014

@martinpaljak Thanks for your interest -- sorry for my late reply. Can you give a more concrete example of the functionality you're seeking?

@martinpaljak
Copy link
Contributor Author

I want some easy and public interface to set arguments after parsing. If one argument (arg2) implies the presence of some other argument (arg1, in my simple code), I don't have to fail ("need arg1 too!"), use intermediate variables (boolean arg1_or_equivalent_present) or OR up arguments in decision points (arg1 || arg2) but just simply "fix" the user interaction by something like "args.setArg(arg1, true)" and use "if (args.has(arg1))" throughout the code. AFAICS there is no public interface for this.

@martinpaljak
Copy link
Contributor Author

The exact use case is reading a parameter that has a default present in input file but could also be given from command line.

@pholser
Copy link
Collaborator

pholser commented Aug 18, 2014

@martinpaljak Thanks for clarifying. Let me think on this one for a while.

@pholser pholser added the 5.x label Aug 18, 2014
@TjlHope
Copy link

TjlHope commented Aug 19, 2014

FYI: I wanted something like this for a pet project I'm working on.
Not sure it's what you'd want, but the route I went was passing a anonymous subclass of a DefaultsGenerator to an overloaded defaultsTo method, it can then programatically generate the defaults from the passed in OptionSet.
WIP and not tested:
https://github.com/TjlHope/jopt-simple/compare/feature/55/defaults-generator

@pholser
Copy link
Collaborator

pholser commented Aug 19, 2014

@TjlHope Neat, thanks! I'll certainly consider this.

@pholser pholser removed the 5.x label Jul 25, 2016
@martinpaljak
Copy link
Contributor Author

I'll try to give it a look. I usually have two passes over the command line arguments in my code: first validate the arguments, make any DWIM adjustments, then on the second pass work on the "tasks" the utility can do, executing them with the right parameters. Not sure if the "default" approach would be sufficient, but worth exploring. From my perspective it would be super easy to just call something like "args.setValue(X, Y)".

@pholser
Copy link
Collaborator

pholser commented Apr 16, 2023

Closing after no activity for a long time.

@pholser pholser closed this as completed Apr 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants