Single dash for long argparse options

Is it possible to make it --longoption

appear like -longoption

using argparse?

argparse.prefix_chars

does not work since it is assumed that the char prefix will be repeated for a long option.

I think maybe there is a way to turn off short options and allow long options to use one dash instead of double dash. Something like that:

parser = argparse.ArgumentParser()
parser.turn_off_short_opts()

      

Can this be done? If not, what can I use to do this?

+3


source to share


2 answers


Single long arguments are not 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 will need to double check the code, but I don't think the difference between long and short options is significant. When an action is created, everything is added to the attribute option_strings

. The long (multichannel) string is used to set "dest", but you can set it yourself as well.

The behavior that Duffy

leads: longoption means a completely different thing: It means -l -o -n -g -p -t -i

is more nuanced. If -l

, -o

etc. All are defined and require no arguments, it will use interpretation. But this does not interfere with regular interpretation -longoption

. But you should be aware of such interpretations and check things out during development.


Here's the code used to set dest

for options:



    if dest is None:
        if long_option_strings:
            dest_option_string = long_option_strings[0]
        else:
            dest_option_string = option_strings[0]

      

before that, he collected the strings --

in the long_option_string list. But if there is no such line, it uses the first line.

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)

      

--longish

Used here instead of -longargument

.

The only other place looking at the length of the option string is special handling -xyz

that focuses on single character strings ('-x', '-y', '-z').

+6


source


Thanks to @CharlesDuffy for the idea.

Go through sys.argv

ahead and replace each -long-option

with --long-option

, but don't forget to intercept the parser's help message as well. Something like that:



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)

      

+1


source







All Articles