ConfigArgParse icon indicating copy to clipboard operation
ConfigArgParse copied to clipboard

Short options are given as example for long options

Open MarcelWaldvogel opened this issue 8 years ago • 4 comments

Given the following code:

import configargparse
p = configargparse.ArgumentParser(default_config_files=['/etc/foo.conf'])
p.add_argument('-u', '--url')
p.parse_args()

Running it with --help results in the following text:

usage: bug.py [-h] [-u URL]

Args that start with '--' (eg. -u) can also be set in a config file
(/etc/foo.conf). Config file syntax allows: key=value, flag=true,
stuff=[a,b,c] (for details, see syntax at https://goo.gl/R74nmi). If an arg is
specified in more than one place, then commandline values override config file
values which override defaults.

optional arguments:
  -h, --help         show this help message and exit
  -u URL, --url URL

The eg. -u should be eg. --url

MarcelWaldvogel avatar Jun 05 '17 12:06 MarcelWaldvogel

you're right, it should print the long option. A pull request / fix would be appreciated.

bw2 avatar Jul 06 '17 23:07 bw2

I already had a look at the code when I created the issue and was unclear about the structure of the options array, so gave up. Could you provide some hints?

MarcelWaldvogel avatar Jul 07 '17 04:07 MarcelWaldvogel

I've just made a test with regular argparse (which, as I understand, is a parent class of ConfigArgParse) https://ideone.com/4PNHk4 This test shows the the behaviour of argparse depends on order of arguments.

import argparse
p = argparse.ArgumentParser()
p.add_argument('--test', '-t')
p.add_argument('-a', '--another-test')
p.parse_args(["-h"])

Output:

usage: prog [-h] [--test TEST] [-a ANOTHER_TEST]

optional arguments:
  -h, --help            show this help message and exit
  --test TEST, -t TEST
  -a ANOTHER_TEST, --another-test ANOTHER_TEST

So I think that ticket should be closed, but this point should be described in documentation

comargo avatar Jul 31 '17 09:07 comargo

A fix is to replace the code here: https://github.com/bw2/ConfigArgParse/blob/8bbc7de67f884184068d62af7f78e723d01c0081/configargparse.py#L806-L807 with

        msg += ("Args that start with '%s' (eg. %s) can also be set in "
                "a config file") % (cc, [c[0] for c in config_settable_args if '--' in c[0]][0])

i.e. replace config_settable_args[0][0] with [c[0] for c in config_settable_args if '--' in c[0]][0]

kaisengit avatar Mar 26 '19 16:03 kaisengit