Change -x and -y options to have their argument optional#21
Change -x and -y options to have their argument optional#21UffeJakobsen wants to merge 1 commit into
Conversation
Options -x and -y becomes reporting options - When no NS argument is supplied
|
Did you have a chance to look at this pull ? :-) |
|
Thanks for the reminder. I like the idea, but the problem is that #!/usr/bin/env python3
import argparse
p = argparse.ArgumentParser()
p.add_argument('filenames', nargs='*')
p.add_argument('--test', nargs='?')
p.add_argument('--other', action='store_true')
args1 = p.parse_args(['foo','bar','--test'])
print(args1)
args2 = p.parse_args(['--test=testARG','foobar'])
print(args2)
args3 = p.parse_args(['--test','testARG','foobar'])
print(args3)
args4 = p.parse_args(['--test','--other'])
print(args4)
args5 = p.parse_args(['--test','will_this_be_the_argument_of_test_or_a_filename?','--other'])
print(args5)Output: In other words, an option with Because of this extremely unintuitive behavior, and because there is no better way to create an "argument-optional" option using the Python standard library… I don't want to use |
|
If you can think of a simple way to implement "argument-optional" options that doesn't exhibit the surprising behavior of the |
|
@dlenski: Thanks for your reply :-) I understand your concern about nargs='?'... Changing the input files from nargs='*' into nargs=argparse.REMAINDER will solve parts of the problem by forcing the files into always appearing as the last elements on the cmd line - but it will just open new problems.... Another more logical approach - would be introducing -X and -Y as options that only does reporting and no changes compared to their lower-case equivalents. Only problem with that is that the -X option is already used to exit without any errorcode Question is if you would accept changing the existing -X option to another one ? |
Yeah, this seems like the most reasonable approach. Good ideas. How about change the existing |
|
Great - I'll rework the pull request :-) |
Options -x and -y becomes reporting options - When no NS argument is supplied