argparse-extd


Nameargparse-extd JSON
Version 0.0.5 PyPI version JSON
download
home_pagehttps://github.com/nanigashi-uji/argparse-extd.git
SummaryExtended argparse.ArgumentParser and Extended argparse.Namespace
upload_time2025-07-18 00:32:48
maintainerNone
docs_urlNone
authorNanigashi Uji
requires_pythonNone
licenseBSD-3-Clause
keywords argparse extenstion
VCS
bugtrack_url
requirements pkgstruct PyYAML toml
Travis-CI No Travis.
coveralls test coverage No coveralls.
            argparse-extd.ArgumentParserExtd
================================

- 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)



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/nanigashi-uji/argparse-extd.git",
    "name": "argparse-extd",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "argparse extenstion",
    "author": "Nanigashi Uji",
    "author_email": "53845049+nanigashi-uji@users.noreply.github.com",
    "download_url": "https://files.pythonhosted.org/packages/a4/1f/104f0beff5cf5c24d169cd8b2b5ba8d6caf843d66b32eca92186952c90cf/argparse-extd-0.0.5.tar.gz",
    "platform": null,
    "description": "argparse-extd.ArgumentParserExtd\n================================\n\n- Extended class of the standard \u201cargparse\u201d class with new features. The\n  extended class of the standard \u201cargparse.namespace\u201d class,\n  ArgumentParserExtd.NamespaceExtd is also defined.\n\n- new features\n\n  - Write the value of the command line options to file w/\n    JSON/YAML/toml/ini format w/ and w/o compression. The file format\n    will be determined automatically from its file name.\n\n  - Save the value of the command line options to file w/\n    JSON/YAML/toml/ini format w/ and w/o compression. The file format\n    will be determined automatically from its file name\n\n  - Prepare the member function to set the frequently used options.\n\n    - \u201c\u2013help\u201d, \u201c\u2013verbose\u201d, \u201c\u2013quiet\u201d\n    - \u201c\u2013save-config\u201d, \u201c\u2013config\u201d\n\n  - Unified argument parser and the entity to keep the output.\n\nRequirement\n-----------\n\n- ``PyYAML``\n- ``toml``\n- ``pkgstruct`` : for example code\n\nUsage\n-----\n\n- Install\n\n::\n\n    % pip install argparse-extd\n\n- see example code\n\nExamples\n--------\n\n- example code: ``ex_argparseextd.py``\n\n::\n\n   #!/usr/bin/env python3\n   # -*- coding: utf-8 -*-\n\n   import pydoc\n   import sys\n   import pkgstruct\n   import argparse_extd\n\n   def main():\n       # 1. Setting the default config-file name\n\n       #   1.a Analyze directory tree and configuration file name using script-name as a default\n       this_script_name = sys.argv[0].removesuffix('.py')\n       pkg_info=pkgstruct.PkgStruct(script_path=this_script_name)\n       config_name_default = pkg_info.script_basename+'.config.json'\n\n       #   1.b Update directory tree and configuration file name when \"--prefix\" is specified\n       #       Disable auto_help(add_help=False) because \"parse_known_args()\" will be used\n       argprsr = argparse_extd.ArgumentParserExtd(add_help=False)\n       argprsr.add_argument('-p', '--prefix', type=str, help='Directory Prefix')\n       argprsr.add_argument('-c', '--default-config', type=str, default=config_name_default, help='Default config filename')\n       #   Analyze argument once only for \"--prefix\" and \"--default-config\"\n       opts,remains=argprsr.parse_known_args()\n       pkg_info=pkgstruct.PkgStruct(prefix=opts.prefix, script_path=this_script_name)\n\n       #   1.c Determine the configuration file path\n       pkg_default_config=pkg_info.concat_path('pkg_statedatadir', 'config', opts.default_config)\n       # 2 Load configuration file\n       argprsr.load_config(pkg_default_config)\n\n       # 3. Set optional argument to enable \"auto_help\"\n       #    (Recovery of \"add_help=False\" at 1.b)\n       argprsr.add_argument_help()\n       # 4. Set optional argument \"--config\" to read settings from configuration file\n       argprsr.add_argument_config()\n\n       # 5. Set optional argument \"--save-config\" to save settings from configuration file, or specified file\n       argprsr.add_argument_save_config(default_path=pkg_default_config)\n\n       # 6. Equivalent to argprsr.add_argument('-v', '--verbose',\n       #                                       action='store_true', help='show verbose messages')\n       argprsr.add_argument_verbose()\n\n       # 7. Equivalent to argprsr.add_argument('-q', '--quiet',\n       #                                       action='store_false', dest='verbose', \n       #                                       help='supress verbose messages')\n       argprsr.add_argument_quiet(dest='verbose')\n\n       # 8. Add argumanets as usual argparse \n       argprsr.add_argument('-H', '--class-help', action='store_true',\n                            help='Show help for ArgumentParserExtd classes')\n       argprsr.add_argument('-s', '--skimmed-output',\n                            action='store_true', help='Active status')\n       argprsr.add_argument('-o', '--output', type=str, help='output filename') \n       argprsr.add_argument('-f', '--dump-format', type=str,\n                            choices=argparse_extd.ArgumentParserExtd.CONFIG_FORMAT,\n                            default='json', help='Output format')\n\n       argprsr.add_argument('-x', '--first-property', type=str, help='CL option 1')\n       argprsr.add_argument('-y', '--second-property', type=str, help='CL option 2')\n       argprsr.add_argument('-z', '--third-property', action='store_true', help='CL option 3')\n\n       argprsr.add_argument('argv', nargs='*', help='non-optional CL arguments')\n\n       # 9. Select options that is not saved to configuration file putput \n       argprsr.append_write_config_exclude(('--prefix', '--default-config', 'verbose',\n                                            '--skimmed-output', '--output', '--save-config', 'argv'))\n       \n       # 9. Analyze commandline as usual\n       #    Call with 'action_help=True' because the constructor was called with 'add_help=False'.\n       args = argprsr.parse_args(action_help=True)\n\n       # CL option can be accessed by 2 way.\n       #   A. properties of the output object from parse_args()\n       #      ex.\n       #      args    = argprsr.parse_args()\n       #      flg_zzz = args.zzz\n       #\n       #   B. properties of the member object 'args' of the entity of ArgumentParserExtd.\n       #      ex.\n       #      flg_zzz = argprsr.args.zzz\n       #\n\n       if argprsr.args.class_help:\n           pydoc.help = pydoc.Helper(output=sys.stdout)\n           help(argparse_extd.ArgumentParserExtd)\n           help(argparse_extd.ArgumentParserExtd.NamespaceExt)\n           help(argparse_extd.ArgumentParserExtd.ConfigActionExt)\n           sys.exit()\n\n       #\n       # 10. Output options to default configutation file.\n       #\n       argprsr.save_config_action()\n\n       if argprsr.args.verbose:\n           print('Prefix              : ', pkg_info.prefix)\n           print('Default config      : ', argprsr.args.default_config)\n\n       print('Default config path : ', pkg_default_config)\n\n       print('Final Namespace: ', argprsr.args)\n       print('Serialized %-4s:\\n----\\n%s\\n----\\n' % (argprsr.args.dump_format.upper(), \n                                                     (argprsr.skimmed_args_to_string(output_format=argprsr.args.dump_format)\n                                                      if argprsr.args.skimmed_output\n                                                      else argprsr.args_to_string(output_format=argprsr.args.dump_format))))\n       #      \n       # 11. Output options to specified file\n       #\n       argprsr.write_config(argprsr.args.output)\n\n   if __name__ == '__main__':\n       main()\n\nOutput examples\n\n- Showing help\n\n::\n\n   % python3 ex_argparseextd.py --help \n\n   usage: ex_argparseextd.py [-p PREFIX] [-c DEFAULT_CONFIG] [-h] [-C CONFIG]\n                             [-S [SAVE_CONFIG]] [-v] [-q] [-H] [-s]\n                             [-o OUTPUT] [-f {ini,yaml,json,toml}]\n                             [-x FIRST_PROPERTY] [-y SECOND_PROPERTY] [-z]\n                             [argv ...]\n\n   positional arguments:\n     argv                  non-optional CL arguments\n\n   options:\n     -p, --prefix PREFIX   Directory Prefix\n     -c, --default-config DEFAULT_CONFIG\n                           Default config filename\n     -h, --help            show this help message and exit\n     -C, --config CONFIG   path of the configuration file to be loaded\n     -S, --save-config [SAVE_CONFIG]\n                           path of the configuration file to be saved\n     -v, --verbose         show verbose messages\n     -q, --quiet           supress verbose messages\n     -H, --class-help      Show help for ArgumentParserExt classes\n     -s, --skimmed-output  Active status\n     -o, --output OUTPUT   output filename\n     -f, --dump-format {ini,yaml,json,toml}\n                           Output format\n     -x, --first-property FIRST_PROPERTY\n                           CL option 1\n     -y, --second-property SECOND_PROPERTY\n                           CL option 2\n     -z, --third-property  CL option 3\n\n- Saving/Read config file\n\n::\n\n   % python3 ex_argparseextd.py -q -x 'prop1' -o sample.json.bz2 \n\n   Default config path :  ***/var/lib/py_encstorage/config/ex_argparseextd.config.json\n   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=[])\n   Serialized JSON:\n   ----\n   {\n       \"prefix\": null,\n       \"default_config\": \"ex_argparseextd.config.json\",\n       \"help\": false,\n       \"config\": null,\n       \"save_config\": null,\n       \"verbose\": false,\n       \"class_help\": false,\n       \"skimmed_output\": false,\n       \"output\": \"sample.json.bz2\",\n       \"dump_format\": \"json\",\n       \"first_property\": \"prop1\",\n       \"second_property\": null,\n       \"third_property\": false,\n       \"argv\": []\n   }\n   ----\n\n   % bunzip2 -c sample.json.bz2\n   {\n       \"class_help\": false,\n       \"dump_format\": \"json\",\n       \"first_property\": \"prop1\",\n       \"second_property\": null,\n       \"third_property\": false\n   }\n\n   % python3 ex_argparseextd.py --config sample.json.bz2\n\n   Default config path :  ***/var/lib/py_encstorage/config/ex_argparseextd.config.json\n   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=[])\n   Serialized JSON:\n   ----\n   {\n       \"prefix\": null,\n       \"default_config\": \"ex_argparseextd.config.json\",\n       \"help\": false,\n       \"config\": null,\n       \"save_config\": null,\n       \"verbose\": false,\n       \"class_help\": false,\n       \"skimmed_output\": false,\n       \"output\": null,\n       \"dump_format\": \"json\",\n       \"first_property\": \"prop1\",\n       \"second_property\": null,\n       \"third_property\": false,\n       \"argv\": []\n   }\n   ----\n\nAuthor\n------\n\n::\n\n   Nanigashi Uji (53845049+nanigashi-uji@users.noreply.github.com)\n   Nanigashi Uji (4423013-nanigashi_uji@users.noreply.gitlab.com)\n\n\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "Extended argparse.ArgumentParser and Extended argparse.Namespace",
    "version": "0.0.5",
    "project_urls": {
        "Homepage": "https://github.com/nanigashi-uji/argparse-extd.git"
    },
    "split_keywords": [
        "argparse",
        "extenstion"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b025c410e3f0177a8e23c4fcc384d089b863971077b9c2aa959c6f3472813b07",
                "md5": "01fd7b3c94b62925f7243f6d9e930738",
                "sha256": "085e833e83ae76b7afc047525d7e4a529310adbc7a935f1dcf2023a9f7e43e26"
            },
            "downloads": -1,
            "filename": "argparse_extd-0.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "01fd7b3c94b62925f7243f6d9e930738",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 11427,
            "upload_time": "2025-07-18T00:32:42",
            "upload_time_iso_8601": "2025-07-18T00:32:42.518885Z",
            "url": "https://files.pythonhosted.org/packages/b0/25/c410e3f0177a8e23c4fcc384d089b863971077b9c2aa959c6f3472813b07/argparse_extd-0.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a41f104f0beff5cf5c24d169cd8b2b5ba8d6caf843d66b32eca92186952c90cf",
                "md5": "371dbe86bc7e014262be4c1f4ab3645f",
                "sha256": "2c269f55a316ba570af297c849e2788e18df75fce69163aeb23315abe1df1f85"
            },
            "downloads": -1,
            "filename": "argparse-extd-0.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "371dbe86bc7e014262be4c1f4ab3645f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14098,
            "upload_time": "2025-07-18T00:32:48",
            "upload_time_iso_8601": "2025-07-18T00:32:48.176666Z",
            "url": "https://files.pythonhosted.org/packages/a4/1f/104f0beff5cf5c24d169cd8b2b5ba8d6caf843d66b32eca92186952c90cf/argparse-extd-0.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-18 00:32:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "nanigashi-uji",
    "github_project": "argparse-extd",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "pkgstruct",
            "specs": []
        },
        {
            "name": "PyYAML",
            "specs": []
        },
        {
            "name": "toml",
            "specs": []
        }
    ],
    "lcname": "argparse-extd"
}
        
Elapsed time: 2.55801s