Create argparse subcommands with decorators.

Overview

python-argparse-subdec

This is a very simple Python package that allows one to create argparse's subcommands via function decorators.

Usage

Create a SubDec object:

import argparse_subdec

# Create the object to collect subcommands via decorators
sd = argparse_subdec.SubDec()

Now, we can define our subcommands. We define a subcommand by decorating a function with method calls to sd, like below:

@sd.add_argument('--option', default='123')
@sd.add_argument('--another')
def foo(args):
    print(f'foo subcommand: --option={args.option!r} and --another={args.another!r}')

You can use any method name that you would use in a subparser. In our example above, we used add_argument, probably most commonly used. When creating the subparsers, sd will replay those calls to the created subparser for the foo subcommand.

In the example below we define a second subcommand, which will be named foo-bar:

@sd.cmd(prefix_chars=':')
@sd.add_argument('positional_arg', type=int)
def foo_bar(args):
    print(f'foo-bar subcommand: {args.positional_arg!r}')

Note that we use a special decorator, sd.cmd, which makes sd pass all of its arguments down to add_parser() when creating the subparser for foo-bar.

Once our subcommands are defined, we must call sd.create_parsers() in order to effectively create the subparsers:

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
sd.create_parsers(subparsers)

The following is example of how we would call the subcommands:

args = parser.parse_args(['foo', '--another', 'hello'])
args.fn(args) # The subcommand function is assigned to ``args.fn`` by default
# Outputs: foo subcommand: --option='123' and --another='hello'

For more details about the API, check out the subdec module.

Install

Via pip:

pip install argparse-subdec
You might also like...
A drop-in replacement for argparse that allows options to also be set via config files and/or environment variables.

ConfigArgParse Overview Applications with more than a handful of user-settable options are best configured through a combination of command line args,

Declarative CLIs with argparse and dataclasses

argparse_dataclass Declarative CLIs with argparse and dataclasses. Features Features marked with a โœ“ are currently implemented; features marked with a

Simple subcommand CLIs with argparse

multicommand Simple subcommand CLIs with argparse. multicommand uses only the standard library and is ~150 lines of code (modulo comments and whitespa

Collection of admin fields and decorators to help to create computed or custom fields more friendly and easy way
Collection of admin fields and decorators to help to create computed or custom fields more friendly and easy way

django-admin-easy Collection of admin fields, decorators and mixin to help to create computed or custom fields more friendly and easy way Installation

Extensible memoizing collections and decorators

cachetools This module provides various memoizing collections and decorators, including variants of the Python Standard Library's @lru_cache function

A Python module for decorators, wrappers and monkey patching.

wrapt The aim of the wrapt module is to provide a transparent object proxy for Python, which can be used as the basis for the construction of function

A lightweight (serverless) native python parallel processing framework based on simple decorators and call graphs.
A lightweight (serverless) native python parallel processing framework based on simple decorators and call graphs.

A lightweight (serverless) native python parallel processing framework based on simple decorators and call graphs, supporting both control flow and dataflow execution paradigms as well as de-centralized CPU & GPU scheduling.

Generate Class & Decorators for your FastAPI project โœจ๐Ÿš€

Classes and Decorators to use FastAPI with class based routing. In particular this allows you to construct an instance of a class and have methods of that instance be route handlers for FastAPI & Python 3.8.

 Python Advanced --- numpy, decorators, networking
Python Advanced --- numpy, decorators, networking

Python Advanced --- numpy, decorators, networking (and more?) Hello everyone ๐Ÿ‘‹ This is the project repo for the "Python Advanced - ..." introductory

Strong Typing in Python with Decorators

typy Strong Typing in Python with Decorators Description This light-weight library provides decorators that can be used to implement strongly-typed be

Decorators for maximizing memory utilization with PyTorch & CUDA

torch-max-mem This package provides decorators for memory utilization maximization with PyTorch and CUDA by starting with a maximum parameter size and

Dude is a very simple framework for writing web scrapers using Python decorators
Dude is a very simple framework for writing web scrapers using Python decorators

Dude is a very simple framework for writing web scrapers using Python decorators. The design, inspired by Flask, was to easily build a web scraper in just a few lines of code. Dude has an easy-to-learn syntax.

Qlib is an AI-oriented quantitative investment platform, which aims to realize the potential, empower the research, and create the value of AI technologies in quantitative investment. With Qlib, you can easily try your ideas to create better Quant investment strategies.
Import entity definition document into SQLie3. Manage the entity. Also, create a "Create Table SQL file".

EntityDocumentMaker Version 1.00 After importing the entity definition (Excel file), store the data in sqlite3. ใ‚จใƒณใƒ†ใ‚ฃใƒ†ใ‚ฃๅฎš็พฉ๏ผˆExcelใƒ•ใ‚กใ‚คใƒซ๏ผ‰ใ‚’ใ‚คใƒณใƒใƒผใƒˆใ—ใŸๅพŒใ€ใƒ‡ใƒผใ‚ฟใ‚’sqlit

Game Agent Framework. Helping you create AIs / Bots that learn to play any game you own!
Game Agent Framework. Helping you create AIs / Bots that learn to play any game you own!

Serpent.AI - Game Agent Framework (Python) Update: Revival (May 2020) Development work has resumed on the framework with the aim of bringing it into 2

Launched in 2018 Actively developed and supported. Supports tkinter, Qt, WxPython, Remi (in browser). Create custom layout GUI's simply.  Python 2.7 & 3 Support. 200+ Demo programs & Cookbook for rapid start. Extensive documentation.  Examples using Machine Learning(GUI, OpenCV Integration,  Chatterbot), Floating Desktop Widgets, Matplotlib + Pyplot integration, add GUI to command line scripts, PDF & Image Viewer. For both beginning and advanced programmers .
ใŠ™๏ธ Create standard barcodes with Python. No external dependencies. 100% Organic Python.

python-barcode python-barcode provides a simple way to create barcodes in Python. There are no external dependencies when generating SVG files. Pillow

Quickly and easily create / train a custom DeepDream model
Quickly and easily create / train a custom DeepDream model

Dream-Creator This project aims to simplify the process of creating a custom DeepDream model by using pretrained GoogleNet models and custom image dat

Turi Create simplifies the development of custom machine learning models.
Turi Create simplifies the development of custom machine learning models.

Quick Links: Installation | Documentation | WWDC 2019 | WWDC 2018 Turi Create Check out our talks at WWDC 2019 and at WWDC 2018! Turi Create simplifie

Releases(v0.2.1)
Owner
Gustavo Josรฉ de Sousa
Gustavo Josรฉ de Sousa
Declarative CLIs with argparse and dataclasses

argparse_dataclass Declarative CLIs with argparse and dataclasses. Features Features marked with a โœ“ are currently implemented; features marked with a

Mike DePalatis 29 Dec 6, 2022
Simple subcommand CLIs with argparse

multicommand Simple subcommand CLIs with argparse. multicommand uses only the standard library and is ~150 lines of code (modulo comments and whitespa

Andrew Ross 10 Aug 1, 2022
The easiest way to create beautiful CLI for your programs.

The Yandere is a program written in Python3, allowing you to create your own beautiful CLI tool.

Billy 31 Dec 20, 2022
Shellmon is a tool used to create and control a webshell remotely, created using the Python3

An Simple PHP Webshell Manager Description Shellmon is a tool used to create and control a webshell remotely, created using the Python3 programming la

22XploiterCrew 12 Dec 30, 2022
A command line tool to create a graph representing your Ansible playbook tasks and roles

Ansible Playbook Grapher ansible-playbook-grapher is a command line tool to create a graph representing your Ansible playbook plays, tasks and roles.

Mohamed El Mouctar Haidara 424 Dec 20, 2022
Create animated ASCII-art for the command line almost instantly!

clippy Create and play colored ?? ?? ?? or colorless โฌ›๏ธ โฌœ๏ธ animated, or static, ASCII-art in the command line! clippy can help if you are wanting to;

Connor 10 Jun 26, 2022
๐ŸThe nx-python plugin allows users to create a basic python application using nx commands.

?? NxPy: Nx Python plugin This project was generated using Nx. The nx-python plugin allows users to create a basic python application using nx command

StandUP Communications 74 Aug 31, 2022
Pynavt is a cli tool to create clean architecture app for you including Fastapi, bcrypt and jwt.

Pynavt _____ _ | __ \ | | | |__) | _ _ __ __ ___ _| |_ | ___/ | | | '_ \ / _` \ \ / /

Alejandro Castillo 1 Dec 13, 2021
Fully Automated YouTube Channel โ–ถ๏ธwith Added Extra Features.

Fully Automated Youtube Channel โ–’โ–ˆโ–€โ–€โ–ˆ โ–ˆโ–€โ–€โ–ˆ โ–€โ–€โ–ˆโ–€โ–€ โ–€โ–€โ–ˆโ–€โ–€ โ–ˆโ–‘โ–‘โ–ˆ โ–ˆโ–€โ–€โ–„ โ–ˆโ–€โ–€ โ–ˆโ–€โ–€โ–ˆ โ–’โ–ˆโ–€โ–€โ–„ โ–ˆโ–‘โ–‘โ–ˆ โ–‘โ–‘โ–ˆโ–‘โ–‘ โ–‘โ–’โ–ˆโ–‘โ–‘ โ–ˆโ–‘โ–‘โ–ˆ โ–ˆโ–€โ–€โ–„ โ–ˆโ–€โ–€ โ–ˆโ–„โ–„โ–€ โ–’โ–ˆโ–„โ–„โ–ˆ โ–€โ–€โ–€โ–€ โ–‘โ–‘โ–€โ–‘โ–‘ โ–‘โ–’โ–ˆโ–‘โ–‘ โ–‘โ–€โ–€โ–€ โ–€โ–€โ–€โ–‘

sam-sepiol 249 Jan 2, 2023
Enriches Click with option groups, constraints, command aliases, help sections for subcommands, themes for --help and other stuff.

Enriches Click with option groups, constraints, command aliases, help sections for subcommands, themes for --help and other stuff.

Gianluca Gippetto 62 Dec 22, 2022