All Projects → krassowski → declarative-parser

krassowski / declarative-parser

Licence: MIT license
Modern, declarative argument parser for Python 3.6+

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to declarative-parser

Clikt
Multiplatform command line interface parsing for Kotlin
Stars: ✭ 1,658 (+5248.39%)
Mutual labels:  argument-parser, argument-parsing, option-parser, command-line-parser
Argagg
A simple C++11 command line argument parser
Stars: ✭ 180 (+480.65%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Argumentparser
Faster, easier, more declarative parsing of command line arguments in Objective-C/Foundation.
Stars: ✭ 251 (+709.68%)
Mutual labels:  argument-parser, argument-parsing, command-line-parser
option-parser
A Lightweight, header-only CLI option parser for C++
Stars: ✭ 16 (-48.39%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Programoptions.hxx
Single-header program options parsing library for C++11
Stars: ✭ 112 (+261.29%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Lyra
A simple to use, composable, command line parser for C++ 11 and beyond
Stars: ✭ 238 (+667.74%)
Mutual labels:  argument-parser, argument-parsing, option-parser
dropt
dropt is yet another C library for parsing command-line options.
Stars: ✭ 39 (+25.81%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Kotlin Argparser
Easy to use and concise yet powerful and robust command line argument parsing for Kotlin
Stars: ✭ 431 (+1290.32%)
Mutual labels:  argument-parser, option-parser, command-line-parser
CmdLine2
Command line argument parser (C++14)
Stars: ✭ 18 (-41.94%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Caporal.js
A full-featured framework for building command line applications (cli) with node.js
Stars: ✭ 3,279 (+10477.42%)
Mutual labels:  argument-parser, argument-parsing, command-line-parser
minimist2
TypeScript/JavaScript ES6 rewrite of popular Minimist argument parser
Stars: ✭ 20 (-35.48%)
Mutual labels:  argument-parser, argparse, command-line-parser
Clipp
easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation
Stars: ✭ 687 (+2116.13%)
Mutual labels:  argument-parser, argument-parsing, option-parser
Clap
Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.
Stars: ✭ 7,174 (+23041.94%)
Mutual labels:  argument-parser, argument-parsing, command-line-parser
argparser
Simple command-line parser for C/C++ programs
Stars: ✭ 50 (+61.29%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Yaap
Yet Another (Swift) Argument Parser
Stars: ✭ 124 (+300%)
Mutual labels:  argument-parser, argument-parsing
Argparse.jl
Package for parsing command-line arguments to Julia programs.
Stars: ✭ 131 (+322.58%)
Mutual labels:  argument-parser, argument-parsing
jsonargparse
Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables
Stars: ✭ 168 (+441.94%)
Mutual labels:  argument-parser, argparse
command-line-commands
Add a git-like command interface to your app.
Stars: ✭ 40 (+29.03%)
Mutual labels:  option-parser, command-line-parser
Argh
Argh! A minimalist argument handler.
Stars: ✭ 752 (+2325.81%)
Mutual labels:  argument-parser, command-line-parser
argparse
Parser for command-line arguments
Stars: ✭ 24 (-22.58%)
Mutual labels:  argument-parser, argument-parsing

Declarative Parser

Build Status Code Climate Coverage Status Documentation Status

Modern, declarative argument parser for Python 3.6+. Powerful like click, integrated like argparse, declarative as sqlalchemy. MIT licenced. Documented on RTD. Install with:

python3 -m pip install declarative_parser

As simple as argparse

It's built on top of argparse - everything you already know stays valid!

from declarative_parser import Parser, Argument

class MyParser(Parser):
    square = Argument(help='display a square of a given number')

parser = MyParser()
args = parser.parse_args()
print(args.square**2)

Nested and Parallel

Everyone knows about nested args. What about parallel groups?

supported_formats = ['png', 'jpeg', 'gif']

class InputOptions(Parser):
    path = Argument(type=argparse.FileType('rb'), optional=False)
    format = Argument(default='png', choices=supported_formats)

class OutputOptions(Parser):
    format = Argument(default='jpeg', choices=supported_formats)
    scale = Argument(type=int, default=100, help='Rescale image to %% of original size')

class ImageConverter(Parser):
    description = 'This app converts images'

    verbose = Argument(action='store_true')
    input = InputOptions()
    output = OutputOptions()

parser = ImageConverter()

commands = '--verbose input image.png output --format gif --scale 50'.split()

namespace = parser.parse_args(commands)

assert namespace.input.format == 'png'
assert namespace.output.format == 'gif'

Intelligent

Make use of Python 3 type hints to reduce tedious task of parsers writing to two or three lines. Positional, keyword arguments, type hints, docstrings - everything can be meaningfully transformed into a parser. And if you decide to take control, just overwrite the automatically deduced arguments with an Argument() defined as a class variable.

import argparse
from declarative_parser import Argument
from declarative_parser.constructor_parser import ConstructorParser

class MyProgram:

    database = Argument(
        type=argparse.FileType('r'),
        help='Path to file with the database'
    )

    def __init__(self, text: str, threshold: float=0.05, database=None):
        """My program does XYZ.

        Arguments:
          threshold: a floating-point value defining threshold, default 0.05
          database: file object to the database if any
        """
        print(text, threshold, None)

parser = ConstructorParser(MyProgram)

options = parser.parse_args()
program = parser.constructor(**vars(options))

And it works quite intuitively:

$ ./my_program.py test --threshold 0.6
test 0.6 None
$ ./my_program.py test --threshold f
usage: my_program.py [-h] [--database DATABASE] [--threshold THRESHOLD] text {} ...
my_program.py: error: argument --threshold: invalid float value: 'f'
$ ./my_program.py --threshold 0.6
usage: my_program.py [-h] [--database DATABASE] [--threshold THRESHOLD] text {} ...
my_program.py: error: the following arguments are required: text

Three docstring formats are supported: Google, NumPy and reStructuredText, with the default being Google.

PS. It works with functions too; see the documentation of FunctionParser.

Practical

What if you only want to show licence of your program? or version? Is there a need to write a separate logic? DeclarativeParser gives you utility decorator: @action which utilizes the power of argparse.Action, leaving behind the otherwise necessary boilerplate code.

__version__ = 2.0

import argparse
from declarative_parser import action
from declarative_parser.constructor_parser import ConstructorParser

class MyProgram:

    def __init__(self, threshold: float=0.05):
        """My program does XYZ.

        Arguments:
          threshold: a floating-point value, default 0.05
        """
        pass

    @action
    def version(options):
       print(__version__)

parser = ConstructorParser(MyProgram)

options = parser.parse_args()
program = parser.constructor(**vars(options))

The execution of an action will (by default) cause the program to exit immediately when finished.

See following run as example:

$ ./my_program.py --version
2.0

See more examples in the documentation.

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].