configargparse - reading option from config only

Richard Damon Richard at Damon-Family.org
Fri Aug 27 20:06:24 EDT 2021


On 8/27/21 3:37 AM, Loris Bennett wrote:
> Richard Damon <Richard at Damon-Family.org> writes:
>
>> On 8/26/21 6:01 AM, Loris Bennett wrote:
>>> Hi,
>>>
>>> When using configargparse, it seems that if a value is to be read from a
>>> config file, it also has to be defined as a command-line argument in
>>> order to turn up as an attribute in the parser namespace.  
>>>
>>> I can sort of see why this is the case, but there are also some options
>>> I would like to read just from the config file and not have them
>>> available as command-line options.  This would be, say, to prevent the
>>> number of options on the command-line from becoming bloated by
>>> little-used settings.
>>>
>>> Is there an elegant way to do this?
>>>
>>> Cheers,
>>>
>>> Loris
>>>
>> Look at the read() member function to supply the file name to read. Then
>> in the config object there will be sections for each section in the
>> config file. No need for any of these to be 'options'
> Do you have a link for this?  As far as I can see, the config files are
> given in the following manner:
>
>   p = configargparse.ArgParser(default_config_files=['/etc/app/conf.d/*.conf', '~/.my_settings'])
>
> I can obviously just read the config file with configparser, but the
> idea of configargparse is that an option can be specified as an option,
> in a config file, or as an environment variable,
>
> As far as I can tell, configargparse only loads entries from the config
> file into the appropriate namespace if they have also been defined as
> long options (i.e. with '--').  I was hoping to access *all* the config
> file entries, regardless of whether they are also options, since the
> config is obviously being read.
>
> Cheers,
>
> Loris
>
I misread your question, I thought you were talking about configparse.

Question is, if configargparse doesn't do what you want, then it isn't
the right tool.

It looks like configargparse is SPECIFICALLY designed to allow the use
to use a file as a shorthand to present command line arguements. The
whole parsing structure is based on an enumerated set of options, if
that isn't what you have, it is the wrong tool.

-- 
Richard Damon



More information about the Python-list mailing list