All Projects → mivade → argparse_dataclass

mivade / argparse_dataclass

Licence: MIT license
Declarative CLIs with argparse and dataclasses

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to argparse dataclass

declarative-parser
Modern, declarative argument parser for Python 3.6+
Stars: ✭ 31 (+3.33%)
Mutual labels:  argparse, command-line-parser
minimist2
TypeScript/JavaScript ES6 rewrite of popular Minimist argument parser
Stars: ✭ 20 (-33.33%)
Mutual labels:  argparse, command-line-parser
jsonargparse
Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
Stars: ✭ 168 (+460%)
Mutual labels:  argparse, dataclasses
Bash Argsparse
An high level argument parsing library for bash
Stars: ✭ 128 (+326.67%)
Mutual labels:  command-line-parser
Commander
🚀The framework to write type-safe and structured command line program easily in Swift.
Stars: ✭ 170 (+466.67%)
Mutual labels:  command-line-parser
docopt-ng
Humane command line arguments parser. Now with maintenance, typehints, and complete test coverage.
Stars: ✭ 94 (+213.33%)
Mutual labels:  argparse
hydra-zen
Pythonic functions for creating and enhancing Hydra applications
Stars: ✭ 165 (+450%)
Mutual labels:  dataclasses
Spectre.cli
An extremely opinionated command-line parser.
Stars: ✭ 121 (+303.33%)
Mutual labels:  command-line-parser
cliar
Create modular Python CLIs with type annotations and inheritance
Stars: ✭ 47 (+56.67%)
Mutual labels:  argparse
autoprogram
Documenting CLI programs
Stars: ✭ 37 (+23.33%)
Mutual labels:  argparse
Argumentparser
Faster, easier, more declarative parsing of command line arguments in Objective-C/Foundation.
Stars: ✭ 251 (+736.67%)
Mutual labels:  command-line-parser
Argagg
A simple C++11 command line argument parser
Stars: ✭ 180 (+500%)
Mutual labels:  command-line-parser
YouTube-Tutorials--Italian
📂 Source Code for (some of) the Programming Tutorials from my Italian YouTube Channel and website ProgrammareInPython.it. This is just a small portion of the content: please visit the website for more.
Stars: ✭ 28 (-6.67%)
Mutual labels:  argparse
Konfig
Simple config properties API for Kotlin
Stars: ✭ 249 (+730%)
Mutual labels:  command-line-parser
Co
Art of C++. Flag, logging, unit-test, json, go-style coroutine and more.
Stars: ✭ 2,264 (+7446.67%)
Mutual labels:  command-line-parser
Typin
Declarative framework for interactive CLI applications
Stars: ✭ 126 (+320%)
Mutual labels:  command-line-parser
argparse-to-class
Transform argparse into class format for Jupyter Notebook execution
Stars: ✭ 20 (-33.33%)
Mutual labels:  argparse
command-line-commands
Add a git-like command interface to your app.
Stars: ✭ 40 (+33.33%)
Mutual labels:  command-line-parser
Cli Matic
Compact, hands-free [sub]command line parsing library for Clojure.
Stars: ✭ 245 (+716.67%)
Mutual labels:  command-line-parser
argparseui
automagically add a PyQt based UI to setup options to an argparse based command-line tool
Stars: ✭ 23 (-23.33%)
Mutual labels:  argparse

argparse_dataclass

Declarative CLIs with argparse and dataclasses.

https://travis-ci.org/mivade/argparse_dataclass.svg?branch=master

PyPI

Features

Features marked with a ✓ are currently implemented; features marked with a ⊘ are not yet implemented.

  • [✓] Positional arguments
  • [✓] Boolean flags
  • [✓] Integer, string, float, and other simple types as arguments
  • [✓] Default values
  • [✓] Arguments with a finite set of choices
  • [⊘] Subcommands
  • [⊘] Mutually exclusive groups

Examples

Using dataclass decorator

>>> from argparse_dataclass import dataclass
>>> @dataclass
... class Options:
...     x: int = 42
...     y: bool = False
...
>>> print(Options.parse_args(['--y']))
Options(x=42, y=True)

A simple parser with flags:

>>> from dataclasses import dataclass
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     verbose: bool
...     other_flag: bool
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args([]))
Options(verbose=False, other_flag=False)
>>> print(parser.parse_args(["--verbose", "--other-flag"]))
Options(verbose=True, other_flag=True)

Using defaults:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     x: int = 1
...     y: int = field(default=2)
...     z: float = field(default_factory=lambda: 3.14)
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args([]))
Options(x=1, y=2, z=3.14)

Enabling choices for an option:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     small_integer: int = field(metadata=dict(choices=[1, 2, 3]))
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["--small-integer", "3"]))
Options(small_integer=3)

Using different flag names and positional arguments:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     x: int = field(metadata=dict(args=["-x", "--long-name"]))
...     positional: str = field(metadata=dict(args=["positional"]))
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["-x", "0", "positional"]))
Options(x=0, positional='positional')
>>> print(parser.parse_args(["--long-name", 0, "positional"]))
Options(x=0, positional='positional')

Using a custom type converter:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     name: str = field(metadata=dict(type=str.title))
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["--name", "john doe"]))
Options(name='John Doe')

Configuring a flag to have a default value of True:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     verbose: bool = True
...     logging: bool = field(default=True, metadata=dict(args=["--logging-off"]))
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args([]))
Options(verbose=True, logging=True)
>>> print(parser.parse_args(["--no-verbose", "--logging-off"]))
Options(verbose=False, logging=False)

Configuring a flag so it is required to set:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     logging: bool = field(metadata=dict(required=True))
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["--logging"]))
Options(logging=True)
>>> print(parser.parse_args(["--no-logging"]))
Options(logging=False)

Parsing only the known arguments:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     name: str
...     logging: bool = False
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_known_args(["--name", "John", "--other-arg", "foo"]))
(Options(name='John', logging=False), ['--other-arg', 'foo'])

Configuring a field with the Optional generic type:

>>> from dataclasses import dataclass, field
>>> from typing import Optional
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     name: str
...     id: Optional[int] = None
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["--name", "John"]))
Options(name='John', id=None)
>>> print(parser.parse_args(["--name", "John", "--id", "1234"]))
Options(name='John', id=1234)

License

MIT License

Copyright (c) 2021 Michael V. DePalatis and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].