python ConfigArgParse Note ~ Are you still managing arguments and config files separately? ~

It was troublesome to manage the command line arguments and the parameters of the configuration file separately, so I thought it would be nice to find a parser that can manage both at once, so I checked it.

ConfigArgParse https://github.com/bw2/ConfigArgParse

Is it used?

If it is not used too much, it will be EOL, so check it lightly

~$ apt-cache rdepends python3-configargparse
python3-configargparse
Reverse Depends:
  python3-certbot
  snakemake
  bdfproxy
  python3-azure-devtools
  prometheus-pgbouncer-exporter
  cloudprint

The most major one is "cerbot" which is a client of Let's Encrypt, or "bdfproxy" for security people, isn't it okay?

Supported formats

Also check the supported formats.

# how to specify a key-value pair (all of these are equivalent):
name value     # key is case sensitive: "Name" isn't "name"
name = value   # (.ini style)  (white space is ignored, so name = value same as name=value)
name: value    # (yaml style)
--name value   # (argparse style)

Confirm that it supports space delimiter, .ini format, and yaml format as standard. I was happy if there was a json format if possible, but it is good because it corresponds to the yaml style that is most used within a radius of 5 m.

Installation

The test environment is ubuntu 19.04 The latest version is 0.14.0 (Jan 13, 2019) in upstream as of October 1, 2019, but I do not want to consider security patches etc. in operation, so I use the package of the above distribution.

$ sudo apt install python3-configargparse
$ dpkg -l python3-configargparse
Request=(U)unknown/(I)Installation/(R)Delete/(P)完全Delete/(H)Retention
|Status=(N)Nothing/(I)Installed/(C)Setting/(U)Deployment/(F)Setting失敗/(H)Semi-install/(W)
|/error?=(Blank)Nothing/(R)Reinstallation required(Status,Uppercase error=Abnormal)
||/Name Version Architecture Description
+++-======================-============-============-===========================
ii python3-configargparse 0.13.0-1 all replacement for argparse wi
lines 1-6/6 (END)

Test code

#!/usr/bin/env python3

import configargparse

def parse_args():
    cp = configargparse.ArgumentParser(
        default_config_files=['./.arg_parse.yaml']
    )
    cp.add_argument(
        '-c', '--conf',
        dest='config',
        is_config_file=True,
        help='config file path',
    )
    cp.add_argument(
        '-f', '--flag',
        dest='flag',
        action="store_true",
        help='true or false flag'
    )
    cp.add_argument(
        '-s', '--str',
        type=str,
        required=True,
        dest='string',
        help='requirement string'
    )
    cp.add_argument(
        '-i', '--int',
        type=int,
        dest='intager',
        default=50,
        help='default intager',
    )
    cp.add_argument(
        '-a', '--array',
        dest='array',
        action='append',
        default=[],
        help='array parama',
    )
    return cp.parse_args()
              
def main():
    args = parse_args()
    print(args)

main()

Configuration file (yaml format example)

$ cat .arg_parse.yaml
str: hogehoge
int: 3
array: [1, 12, 35, 40]

Execution result 1 Configuration file only status

$ ./test_conf.py
Namespace(array=['1', '12', '35', '40'], config=None, flag=False, intager=3, string='hogehoge')

Execution result 2 When both configuration file and command line arguments are used

$ ./test_conf.py -i 1111 -s fugafuga -f
Namespace(array=['1', '12', '35', '40'], config=None, flag=True, intager=1111, string='fugafuga')

Various points

  1. The parameter name of the configuration file is the "int" part of "--int" and is not the "intager" specified by dest.
  2. When int type is used, it is processed as a character string unless "type = int" is entered. Similarly, if you want to specify the str type, you must use "type = str". I thought it was, but even if I put a number in the argument, it is processed as a character string.
  3. "action ='append'" is required when using list as an argument.
  4. When using a boolean value as an argument, "action ='store_true'" or "action ='store_false'" is required.

"Store_true" defaults to "False", and "store_false" defaults to "True". I was confused if it was the opposite, but it seems good to think that it will be True and False respectively when specified by command line arguments.

Impressions

I think it's nice to manage argparse and configparse separately and not be addicted to changing only one.

Recommended Posts

python ConfigArgParse Note ~ Are you still managing arguments and config files separately? ~
What are you comparing with Python is and ==?
Notes on writing config files for Python Note: configparser
python Note: Determine if command line arguments are in the list