yiwangai1983 / argparse

Automatically exported from code.google.com/p/argparse
Other
0 stars 0 forks source link

'-' should be a valid flag #46

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
parser = argparser.ArgumentParser()
parser.add_argument("-")
parser.parser_args()

What is the expected output? What do you see instead?
Expect a single '-' to be a valid argument just as '+' is considered valid.
Errors out with "must contain characters other than '-'"

This probably seems like it should be uncommon, but a single '-' flag is
something I use regularly in my command line scripts. It's so quick and
easy to type that it makes for a very ideal shortcut, like 'cd -'. I also
have many commands that have add/remove behavior, and in all of these cases
I use +/- flags as shortcut flags to '--add' or '--remove'.
Ideally, an argument parser should place no restrictions on the format of
flags whatsoever. I often can't use the builtin optparse exactly because of
the restrictions it places on flag names.

In the way of suggestions, something that could cover this case and at the
same time many other cases would be to allow flags to be regex objects. I.e.,
parser.add_argument(re.compile("foo"))

Original issue reported on code.google.com by sgusta...@gmail.com on 28 Nov 2009 at 12:27

GoogleCodeExporter commented 8 years ago
Could you give me a couple test cases of what you want to work? I'm a little 
confused
by your code vs your examples. For example, 'cd -' should already work fine:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('dir')
>>> parser.parse_args(['-'])
Namespace(dir='-')

For the case where you use +/- alone as flags, do you want to use the + and - 
with
spaces, e.g.:

>>> # hypothetical
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-', dest='add')
>>> parser.parse_args('- X'.split())
Namespace(add='X')

Or do you want to be able to squeeze the argument up against the +/- like this:

>>> # hypothetical
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-')
>>> parser.parse_args('-X'.split())
Namespace(add='X')

The former is relatively easy to support. The latter would be difficult because 
'-X'
looks like a mis-typed flag. I guess it would be possible to just disable the
mis-typed flag detection if an argument like the above is added, but I'd have to
think about it more.

Original comment by steven.b...@gmail.com on 7 Dec 2009 at 12:09

GoogleCodeExporter commented 8 years ago
Just the former. I could see too much confusion easily arising from the use if 
there
wasn't a space in-between. Mostly I just want to be able to treat it as a 
standalone
bool flag or as the key to a subparser. I've used both forms in my scripts, but 
the
subparser form is probably more common.

Original comment by sgusta...@gmail.com on 7 Dec 2009 at 12:20

GoogleCodeExporter commented 8 years ago
Added in r84:

>>> parser = argparse.ArgumentParser(prefix_chars='+-')
>>> parser.add_argument('+', dest='add')
>>> parser.add_argument('-', dest='sub')
>>> parser.parse_args(['+', '42', '-', '3'])
Namespace(add='42', sub='3')

Original comment by steven.b...@gmail.com on 28 Feb 2010 at 8:08