Skip to main content

Extended argparse.ArgumentParser and Extended argparse.Namespace

Project description

  • Extended class of the standard “argparse” class with new features. The extended class of the standard “argparse.namespace” class, ArgumentParserExtd.NamespaceExtd is also defined.

  • new features

    • Write the value of the command line options to file w/ JSON/YAML/toml/ini format w/ and w/o compression. The file format will be determined automatically from its file name.

    • Save the value of the command line options to file w/ JSON/YAML/toml/ini format w/ and w/o compression. The file format will be determined automatically from its file name

    • Prepare the member function to set the frequently used options.

      • “–help”, “–verbose”, “–quiet”

      • “–save-config”, “–config”

    • Unified argument parser and the entity to keep the output.

Requirement

  • PyYAML

  • toml

  • pkgstruct : for example code

Usage

  • Install

% pip install argparse-extd
  • see example code

Examples

  • example code: ex_argparseextd.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pydoc
import sys
import pkgstruct
import argparse_extd

def main():
    # 1. Setting the default config-file name

    #   1.a Analyze directory tree and configuration file name using script-name as a default
    this_script_name = sys.argv[0].removesuffix('.py')
    pkg_info=pkgstruct.PkgStruct(script_path=this_script_name)
    config_name_default = pkg_info.script_basename+'.config.json'

    #   1.b Update directory tree and configuration file name when "--prefix" is specified
    #       Disable auto_help(add_help=False) because "parse_known_args()" will be used
    argprsr = argparse_extd.ArgumentParserExtd(add_help=False)
    argprsr.add_argument('-p', '--prefix', type=str, help='Directory Prefix')
    argprsr.add_argument('-c', '--default-config', type=str, default=config_name_default, help='Default config filename')
    #   Analyze argument once only for "--prefix" and "--default-config"
    opts,remains=argprsr.parse_known_args()
    pkg_info=pkgstruct.PkgStruct(prefix=opts.prefix, script_path=this_script_name)

    #   1.c Determine the configuration file path
    pkg_default_config=pkg_info.concat_path('pkg_statedatadir', 'config', opts.default_config)
    # 2 Load configuration file
    argprsr.load_config(pkg_default_config)

    # 3. Set optional argument to enable "auto_help"
    #    (Recovery of "add_help=False" at 1.b)
    argprsr.add_argument_help()
    # 4. Set optional argument "--config" to read settings from configuration file
    argprsr.add_argument_config()

    # 5. Set optional argument "--save-config" to save settings from configuration file, or specified file
    argprsr.add_argument_save_config(default_path=pkg_default_config)

    # 6. Equivalent to argprsr.add_argument('-v', '--verbose',
    #                                       action='store_true', help='show verbose messages')
    argprsr.add_argument_verbose()

    # 7. Equivalent to argprsr.add_argument('-q', '--quiet',
    #                                       action='store_false', dest='verbose',
    #                                       help='supress verbose messages')
    argprsr.add_argument_quiet(dest='verbose')

    # 8. Add argumanets as usual argparse
    argprsr.add_argument('-H', '--class-help', action='store_true',
                         help='Show help for ArgumentParserExtd classes')
    argprsr.add_argument('-s', '--skimmed-output',
                         action='store_true', help='Active status')
    argprsr.add_argument('-o', '--output', type=str, help='output filename')
    argprsr.add_argument('-f', '--dump-format', type=str,
                         choices=argparse_extd.ArgumentParserExtd.CONFIG_FORMAT,
                         default='json', help='Output format')

    argprsr.add_argument('-x', '--first-property', type=str, help='CL option 1')
    argprsr.add_argument('-y', '--second-property', type=str, help='CL option 2')
    argprsr.add_argument('-z', '--third-property', action='store_true', help='CL option 3')

    argprsr.add_argument('argv', nargs='*', help='non-optional CL arguments')

    # 9. Select options that is not saved to configuration file putput
    argprsr.append_write_config_exclude(('--prefix', '--default-config', 'verbose',
                                         '--skimmed-output', '--output', '--save-config', 'argv'))

    # 9. Analyze commandline as usual
    #    Call with 'action_help=True' because the constructor was called with 'add_help=False'.
    args = argprsr.parse_args(action_help=True)

    # CL option can be accessed by 2 way.
    #   A. properties of the output object from parse_args()
    #      ex.
    #      args    = argprsr.parse_args()
    #      flg_zzz = args.zzz
    #
    #   B. properties of the member object 'args' of the entity of ArgumentParserExtd.
    #      ex.
    #      flg_zzz = argprsr.args.zzz
    #

    if argprsr.args.class_help:
        pydoc.help = pydoc.Helper(output=sys.stdout)
        help(argparse_extd.ArgumentParserExtd)
        help(argparse_extd.ArgumentParserExtd.NamespaceExt)
        help(argparse_extd.ArgumentParserExtd.ConfigActionExt)
        sys.exit()

    #
    # 10. Output options to default configutation file.
    #
    argprsr.save_config_action()

    if argprsr.args.verbose:
        print('Prefix              : ', pkg_info.prefix)
        print('Default config      : ', argprsr.args.default_config)

    print('Default config path : ', pkg_default_config)

    print('Final Namespace: ', argprsr.args)
    print('Serialized %-4s:\n----\n%s\n----\n' % (argprsr.args.dump_format.upper(),
                                                  (argprsr.skimmed_args_to_string(output_format=argprsr.args.dump_format)
                                                   if argprsr.args.skimmed_output
                                                   else argprsr.args_to_string(output_format=argprsr.args.dump_format))))
    #
    # 11. Output options to specified file
    #
    argprsr.write_config(argprsr.args.output)

if __name__ == '__main__':
    main()

Output examples

  • Showing help

% python3 ex_argparseextd.py --help

usage: ex_argparseextd.py [-p PREFIX] [-c DEFAULT_CONFIG] [-h] [-C CONFIG]
                          [-S [SAVE_CONFIG]] [-v] [-q] [-H] [-s]
                          [-o OUTPUT] [-f {ini,yaml,json,toml}]
                          [-x FIRST_PROPERTY] [-y SECOND_PROPERTY] [-z]
                          [argv ...]

positional arguments:
  argv                  non-optional CL arguments

options:
  -p, --prefix PREFIX   Directory Prefix
  -c, --default-config DEFAULT_CONFIG
                        Default config filename
  -h, --help            show this help message and exit
  -C, --config CONFIG   path of the configuration file to be loaded
  -S, --save-config [SAVE_CONFIG]
                        path of the configuration file to be saved
  -v, --verbose         show verbose messages
  -q, --quiet           supress verbose messages
  -H, --class-help      Show help for ArgumentParserExt classes
  -s, --skimmed-output  Active status
  -o, --output OUTPUT   output filename
  -f, --dump-format {ini,yaml,json,toml}
                        Output format
  -x, --first-property FIRST_PROPERTY
                        CL option 1
  -y, --second-property SECOND_PROPERTY
                        CL option 2
  -z, --third-property  CL option 3
  • Saving/Read config file

% python3 ex_argparseextd.py -q -x 'prop1' -o sample.json.bz2

Default config path :  ***/var/lib/py_encstorage/config/ex_argparseextd.config.json
Final Namespace:  NamespaceExt(prefix=None, default_config='ex_argparseextd.config.json', help=False, config=None, save_config=None, verbose=False, class_help=False, skimmed_output=False, output='sample.json.bz2', dump_format='json', first_property='prop1', second_property=None, third_property=False, argv=[])
Serialized JSON:
----
{
    "prefix": null,
    "default_config": "ex_argparseextd.config.json",
    "help": false,
    "config": null,
    "save_config": null,
    "verbose": false,
    "class_help": false,
    "skimmed_output": false,
    "output": "sample.json.bz2",
    "dump_format": "json",
    "first_property": "prop1",
    "second_property": null,
    "third_property": false,
    "argv": []
}
----

% bunzip2 -c sample.json.bz2
{
    "class_help": false,
    "dump_format": "json",
    "first_property": "prop1",
    "second_property": null,
    "third_property": false
}

% python3 ex_argparseextd.py --config sample.json.bz2

Default config path :  ***/var/lib/py_encstorage/config/ex_argparseextd.config.json
Final Namespace:  NamespaceExt(prefix=None, default_config='ex_argparseextd.config.json', help=False, config=None, save_config=None, verbose=False, class_help=False, skimmed_output=False, output=None, dump_format='json', first_property='prop1', second_property=None, third_property=False, argv=[])
Serialized JSON:
----
{
    "prefix": null,
    "default_config": "ex_argparseextd.config.json",
    "help": false,
    "config": null,
    "save_config": null,
    "verbose": false,
    "class_help": false,
    "skimmed_output": false,
    "output": null,
    "dump_format": "json",
    "first_property": "prop1",
    "second_property": null,
    "third_property": false,
    "argv": []
}
----

Author

Nanigashi Uji (53845049+nanigashi-uji@users.noreply.github.com)
Nanigashi Uji (4423013-nanigashi_uji@users.noreply.gitlab.com)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

argparse-extd-0.0.5.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argparse_extd-0.0.5-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file argparse-extd-0.0.5.tar.gz.

File metadata

  • Download URL: argparse-extd-0.0.5.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for argparse-extd-0.0.5.tar.gz
Algorithm Hash digest
SHA256 2c269f55a316ba570af297c849e2788e18df75fce69163aeb23315abe1df1f85
MD5 371dbe86bc7e014262be4c1f4ab3645f
BLAKE2b-256 a41f104f0beff5cf5c24d169cd8b2b5ba8d6caf843d66b32eca92186952c90cf

See more details on using hashes here.

File details

Details for the file argparse_extd-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: argparse_extd-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for argparse_extd-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 085e833e83ae76b7afc047525d7e4a529310adbc7a935f1dcf2023a9f7e43e26
MD5 01fd7b3c94b62925f7243f6d9e930738
BLAKE2b-256 b025c410e3f0177a8e23c4fcc384d089b863971077b9c2aa959c6f3472813b07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page