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


  • Automatic cli discovery and command plugin

    # Folder structure
    ├──         <= empty
    ├── editor/
       ├──     <= ParentCommand(editor)
       ├──         <= Command(cook)
       ├──       <= Command(client)
       ├──         <= Command(game)
       └──         <= Command(open)
    └── uat/
       ├──     <= ParentCommand(uat)
       ├──     <= Command(localize)
       └──         <= Command(test)
    #  editor/
    from argklass.command import ParentCommand
    class Editor(ParentCommand):
       name = "editor"
    COMMANDS = Editor
    from argklass.command import Command
    class Cook(Command):
       name = "cook"
       def execute(args) -> int:
    COMMANDS = Cook
    cli = CommandLineInterface(project.cli)
    # or["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
    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()
    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

    class SubArgs:
       aa: str = argument(default="123")
    class cmd1:
       args: str = "str1"
    class cmd2:
       args: str = "str2"
    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()
    args = parser.parse_args()


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.4.4.tar.gz (20.3 kB view hashes)

Uploaded Source

Built Distribution

argklass-1.4.4-py3-none-any.whl (21.1 kB view hashes)

Uploaded Python 3

Supported by

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