bw2 / ConfigArgParse

A drop-in replacement for argparse that allows options to also be set via config files and/or environment variables.
MIT License
728 stars 121 forks source link

Hardcoded description text cannot be modified #193

Closed fdw closed 1 year ago

fdw commented 4 years ago

I have a short description text in my config, like this: parser = configargparse.ArgumentParser( description='Just one sentence to describe my utility'. Now I noticed that ConfigArgParse appends several sentences to this, completely drowning out my (purposely short) description. I cannot seem to find a switch to change this behavior. Neither can I append line breaks (\n) to make it easier to read.

Is there a way to configure this?

dbrnz commented 4 years ago

I have the same issue...

bw2 commented 3 years ago

I'm not sure I understand the issue. If I run

test.py

import configargparse

p = configargparse.ArgumentParser(description='Just one sentence to describe my utility')
p.add_arg("test")
p.parse_args()

python3 test.py --help

It outputs

usage: test.py [-h] test

Just one sentence to describe my utility

positional arguments:
  test

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

which seems fine.

dbrnz commented 3 years ago

Please try:

import configargparse

p = configargparse.ArgumentParser(description='Just one sentence to describe my utility')
p.add('-c', '--my-config', required=True, is_config_file=True, help='config file path')
p.add('--genome', required=True, help='path to genome file')
p.parse_args()

I get:

usage: test.py [-h] -c MY_CONFIG --genome GENOME

Just one sentence to describe my utility Args that start with '--' (eg. --genome) can also be set in a config file (specified via -c).
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
  -c MY_CONFIG, --my-config MY_CONFIG
                        config file path
  --genome GENOME       path to genome file
bw2 commented 3 years ago

ok, how about adding an ArgParser constructor option to customize the config file-related help text - for example a config_global_help_message arg below https://github.com/bw2/ConfigArgParse/blob/master/configargparse.py#L387 and it would be set to None by default.

Then @ https://github.com/bw2/ConfigArgParse/blob/master/configargparse.py#L910-L924 if the value is None it would print the current message. If the value is not None, it overrides the defaults message. You could then set it to "".

fdw commented 3 years ago

That would solve my problem, yes :)

dbrnz commented 3 years ago

Yes to a constructor option that would allow config file related text to be modified or suppressed.

Isn't there also a formatting issue? Shouldn't the above read:

Just one sentence to describe my utility

Args that start with '--' (eg. --genome) can also be set in a config file (specified via -c).
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.

In fact, would things not read better if the config file text was at the end of the help output? That is:

usage: test.py [-h] -c MY_CONFIG --genome GENOME

Just one sentence to describe my utility

optional arguments:
  -h, --help            show this help message and exit
  -c MY_CONFIG, --my-config MY_CONFIG
                        config file path
  --genome GENOME       path to genome file

Args that start with '--' (eg. --genome) can also be set in a config file (specified via -c).
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.
bw2 commented 3 years ago

I like the idea of moving the text to the end of the help output. Would either of you guys be willing to submit a PR for this?

dbrnz commented 3 years ago

See PR #208.

bw2 commented 3 years ago

@dbrnz 's PR is now merged. Will wait a couple days to see if PR #210 gets fixed, then make a new release.