Is it possible to make it so that --longoption
is represented as -longoption
using argparse?
argparse.prefix_chars
doesn't work, as it is assumed that the prefix char would be repeated for a long option.
I'm thinking perhaps there is a way to turn off short options and allow long options to use a single dash instead of a double dash. Something like this:
parser = argparse.ArgumentParser()
parser.turn_off_short_opts()
Can this be done? If not, what can I use to accomplish this?
To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.
What Does It Do? A double-dash in a shell command signals the end of options and disables further option processing. Let's see this in action using the grep command. Imagine a scenario where we'd like to search for the occurrences of hello in the file data.txt: $ grep hello data.txt.
The argparse module provides a convenient interface to handle command-line arguments. It displays the generic usage of the program, help, and errors. The parse_args() function of the ArgumentParser class parses arguments and adds value as an attribute dest of the object.
Single dash long arguments aren't a problem:
In [250]: p=argparse.ArgumentParser()
In [251]: p.add_argument('-longargument')
Out[251]: _StoreAction(option_strings=['-longargument'], dest='longargument', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
In [252]: p.parse_args(['-long','test'])
Out[252]: Namespace(longargument='test')
In [253]: p.parse_args(['-l','test'])
Out[253]: Namespace(longargument='test')
I'd have to double check the code, but I don't think the distinction between long and short options is that significant. When creating the action, all are added to the option_strings
attribute. The long (multicharacter) string is used to set the 'dest', but you can also set that yourself.
The behavior that Duffy
cites: longoption means a completely different thing: It means -l -o -n -g -p -t -i
is more nuanced. If -l
,-o
, etc are all defined and don't require arguments it will use interpretation. But it doesn't interfer with regular interpretation of -longoption
. But you should be aware of such interpretations, and test things during development.
Here's the code used to set the dest
for optionals:
if dest is None:
if long_option_strings:
dest_option_string = long_option_strings[0]
else:
dest_option_string = option_strings[0]
just before this it collected the strings with --
into the long_option_string` list. But if there isn't such a string, it uses the first string.
In [270]: p.add_argument('-longargument','-l','--longish')
Out[270]: _StoreAction(option_strings=['-longargument', '-l', '--longish'], dest='longish', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
Here --longish
was used instead of -longargument
.
The only other place that looks at option string length is the special handling of the -xyz
, which focuses on the single character strings ('-x', '-y', '-z').
Thanks to @CharlesDuffy for the idea.
Just go through sys.argv
beforehand and replace each -long-option
with --long-option
, but remember to intercept the parser's help message too. Something like this:
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument(('-h', '--help'), help='Show this message and quit', action='count')
# add other args
new_argv = []
for arg in sys.argv:
if arg.startswith('-') and len(arg) > 2:
arg = '-' + arg
new_argv.append(arg)
sys.argv = new_argv
args = parser.parse_args()
if args.help:
help_string = parser.format_help()
print(help_string.replace('--', '-'))
parser.exit(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With