Code Monkey home page Code Monkey logo

arcparse's Introduction

arcparse

Declare program arguments in a type-safe way.

This project builds on top of argparse by adding type-safety and allowing a more expressive argument parser definition.

Example usage

from arcparse import arcparser, flag
from pathlib import Path

@arcparser
class Args:
    path: Path
    recurse: bool = flag("-r")
    item_limit: int = 100
    output_path: Path | None

args = Args.parse()
print(f"Scanning {args.path}...")
...

For more examples see Examples.

Installation

# Using pip
$ pip install arcparse

Features

  • Positional, Option and Flag arguments
  • Multiple values per argument
  • Name overriding
  • Type conversions
  • Mutually exclusive groups
  • Subparsers
  • Parser inheritance

Credits

This project was inspired by swansonk14/typed-argument-parser.

Known issues

Annotations

from __future__ import annotations makes all annotations strings at runtime. This library relies on class variable annotations's types being actual types. inspect.get_annotations(obj, eval_str=True) is used to evaluate string annotations to types in order to assign converters. If an argument is annotated with a non-builtin type which is defined outside of the argument-defining class body the type can't be found which results in NameErrors. This is avoidable either by only using custom types which have been defined in the argument-defining class body (which is restrictive), or alternatively by not using the annotations import which should not be necessary from python 3.13 forward thanks to PEP 649.

arcparse's People

Contributors

kuba314 avatar

Stargazers

Omelug avatar Krazy Bug avatar  avatar  avatar Jarek Prokop avatar

Watchers

 avatar Omelug avatar

arcparse's Issues

Use semantic versioning

For now, this project uses the following versioning 0.x.y, where a change in x is an incompatible API change and a change in y is every thing other, i.e. new features and bug fixes.

Once this project is mature enough, version 1.0.0 will be released and after that semantic versioning will be used.

Support parser inheritance

argparse supports setting a parser as a parent parser for another parser. The inheriting parser gets all arguments automatically from the parent parser.

We currently can't support this through simple inheritance since the @arcparser decorator transforms the shape object into a Parser instance which is an object and can't be subclassed.

There are 2 options how this could be implemented:

  1. @arcparser decorator could accept a collection of parent parser shapes: @arcparser(ParentArgs1, ParentArgs2)
  2. the resulting parser could be implemented as a new class and not an object. This would require a vast rewrite of the current parser code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.