Mailing List Archive

configargparse - reading option from config only
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

--
This signature is currently under construction.
--
https://mail.python.org/mailman/listinfo/python-list
Re: configargparse - reading option from config only [ In reply to ]
Hello,

you could use the argparse module[1] to parse command line arguments and
configparser[2] to parse configuration files following the ini file
structure. Is that what you are expecting?

Cheers


[1]: https://docs.python.org/3/library/argparse.html
[2]: https://docs.python.org/3/library/configparser.html
--
https://mail.python.org/mailman/listinfo/python-list
Re: configargparse - reading option from config only [ In reply to ]
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'

--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list
Re: configargparse - reading option from config only [ In reply to ]
Richard Damon <Richard@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

--
This signature is currently under construction.
--
https://mail.python.org/mailman/listinfo/python-list
Re: configargparse - reading option from config only [ In reply to ]
tuxifreund <kaiser.barbarossa@yandex.com> writes:

> Hello,
>
> you could use the argparse module[1] to parse command line arguments and
> configparser[2] to parse configuration files following the ini file
> structure. Is that what you are expecting?

I have used the combination of argparse and configparser before.
However, I was hoping to just use configargparse instead.

Cheers,

Loris


>
> Cheers
>
>
> [1]: https://docs.python.org/3/library/argparse.html
> [2]: https://docs.python.org/3/library/configparser.html
--
Dr. Loris Bennett (Hr./Mr.)
ZEDAT, Freie Universit├Ąt Berlin Email loris.bennett@fu-berlin.de
--
https://mail.python.org/mailman/listinfo/python-list
Re: configargparse - reading option from config only [ In reply to ]
On 8/27/21 3:37 AM, Loris Bennett wrote:
> Richard Damon <Richard@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

--
https://mail.python.org/mailman/listinfo/python-list
Re: configargparse - reading option from config only [ In reply to ]
Richard Damon <Richard@Damon-Family.org> writes:

> On 8/27/21 3:37 AM, Loris Bennett wrote:
>> Richard Damon <Richard@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.

I am not sure what you mean by using

a file as a shorthand to present command line arguements

since the command-line arguments are defined by caling the 'add_argument'
method of the configargparse object. However, I agree with your
analysis that configargparse is the wrong tool for what I want to do.

I like the idea that a variable can be defined as a command-line option,
an entry in a config file, or as an environment variable. However, when
I think about it, it seems that command-line options are essentially
different from parameters in a configuration file, not least because the
former need some sort of description for the output of '--help', whereas
the latter do not.

Cheers,

Loris
--
This signature is currently under construction.
--
https://mail.python.org/mailman/listinfo/python-list