Skip to main content

Argparse generator from dataclass

Project description

Current PyPi Version Supported Python Versions codecov docs tests style

Inspired by Simple Parsing, simplified and extended to build extensive, extendable command line interface without much code.

pip install argklass

Features

  • Automatic cli discovery and command plugin

    # Folder structure
    project/cli/
    ├── __init__.py         <= empty
    ├── editor/
    │   ├── __init__.py     <= ParentCommand(editor)
    │   ├── cook.py         <= Command(cook)
    │   ├── client.py       <= Command(client)
    │   ├── game.py         <= Command(game)
    │   └── open.py         <= Command(open)
    └── uat/
       ├── __init__.py     <= ParentCommand(uat)
       ├── localize.py     <= Command(localize)
       └── test.py         <= Command(test)
    
    #  editor/__init__.py
    from argklass.command import ParentCommand
    
    class Editor(ParentCommand):
       name = "editor"
    
    
    COMMANDS = Editor
    
    # cook.py
    from argklass.command import Command
    
    class Cook(Command):
       name = "cook"
    
       @staticmethod
       def execute(args) -> int:
          print("cook")
    
    COMMANDS = Cook
    
    #
    cli = CommandLineInterface(project.cli)
    cli.run()
    
    # or
    cli.run(["editor", "cook", "--help"])
    
    #
    cli editor cook --help
    cli uat localize --help
  • New Argument format
    • able to show the entire command line interface with all its subparsers

    • new format mirror dataclass syntax

    editor                                           Set of commands to launch the editors in different modes
       server                                       Parameters added to the Map URL
       game                                         docstring ...
       client                                       docstring ...
       resavepackages                               docstring ...
       cook                                         docstring ...
       ml                                           Launch unreal engine with mladapter setup
       editor                                       Other arguments
       open                                         docstring ...
       localize                                     docstring ...
       worldpartition                               Convert a UE4 map using world partition
       -h, --help                                   Show help
    engine                                           Set of commands to manage engine installation/source
          add                                          docstring ...
          update                                       Update the engine source code
    format                                             docstring ...
          --profile: str                               docstring ...
          --file: str                                  docstring ...
          --fail_on_error: bool = False                docstring ...
          --col: int = 24                              docstring ...
  • Compact argparse definition

    def workdir():
       d = os.getcwd()
       if os.access(d, os.W_OK):
          return d
       return None
    
    
    @dataclass
    class MyArguments:
       a  : str                                                    # Positional
       b  : int                = 20                                # My argument
       c  : bool               = False                             # My argument
       d  : int                = choice(0, 1, 2, 3, 4, default=1)  # choices
       e  : List[int]          = argument(default=[0])             # list
       f  : Optional[int]      = None                              # Optional
       p  : Tuple[int, int]    = (1, 1)                            # help p
       g  : Color              = Color.RED                         # help g
       s  : SubArgs            = SubArgs                           # helps group
       cmd: Union[cmd1, cmd2]  = subparsers(cmd1=cmd1, cmd2=cmd2)  # Command subparser
       de : str                = deduceable(workdir)
    
    parser = ArgumentParser()
    parser.add_arguments(MyArguments)
    args = parser.parse_args()
  • Save and load arguments from configuration files

    parser = build_parser(commands)
    
    # load/save defaults before parsing
    save_defaults(parser, "config.hjson")
    apply_defaults(parser, "config.hjson")
    
    args = parser.parse_args(["editor", "editor"])
    
    # load save arguments after parsing
    save_as_config(parser, args, "dump.hjson")
    apply_config(parser, args, "dump.hjson")
  • Lower level interface, that gives you back all of argparse power

    @dataclass
    class SubArgs:
       aa: str = argument(default="123")
    
    
    @dataclass
    class cmd1:
       args: str = "str1"
    
    
    @dataclass
    class cmd2:
       args: str = "str2"
    
    
    @dataclass
    class MyArguments:
       a: str                  = argument(help="Positional")
       b: int                  = argument(default=20, help="My argument")
       c: bool                 = argument(action="store_true", help="My argument")
       d: int                  = argument(default=1, choices=[0, 1, 2, 3, 4], help="choices")
       e: List[int]            = argument(default=[0], help="list")
       f: Optional[int]        = argument(default=None, help="Optional")
       p: Tuple[int, int]      = argument(default=(1, 1), help="help p")
       g: Color                = argument(default=Color.RED, help="help g")
       s: SubArgs              = group(default=SubArgs, help="helps group")
       cmd: Union[cmd1, cmd2]  = subparsers(cmd1=cmd1, cmd2=cmd2)
    
    
    parser = ArgumentParser()
    parser.add_arguments(MyArguments)
    args = parser.parse_args()

Architecture

argklass works by building the argument parser as a tree, adding metadata to each nodes when necessary.

One of the core component is ArgumentParserIterator which traverse the parsing tree. Each features, such as argument grouping into dataclasses or saving/loading configuration, are implemented as a simple traversal.

This enable us to implement each feature independently from each other and make them optional.

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

argklass-1.6.0.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

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

argklass-1.6.0-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file argklass-1.6.0.tar.gz.

File metadata

  • Download URL: argklass-1.6.0.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for argklass-1.6.0.tar.gz
Algorithm Hash digest
SHA256 b0c67cda2755b7e73dcda6c0249ceb9f59e57d98b1dd59ab96f8f5662a05e119
MD5 6b9b5a78be1bf980f1873d60197aa302
BLAKE2b-256 dca72e779039942f44bb6ea3d703bdd8ccdef80f1200928f21392a8edf983648

See more details on using hashes here.

File details

Details for the file argklass-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: argklass-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for argklass-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df7b5791ff89cc3b0feb10ade31aa390100001265cc4ab4a927f68bc5b8a5176
MD5 a481877786f9831769abdf1c28c98634
BLAKE2b-256 25ab17904a30f6f4dd5c778f4f9315fc1de7f358b8087674efde5571a840c35e

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