{"id":20456553,"url":"https://github.com/krassowski/declarative-parser","last_synced_at":"2026-02-28T22:31:55.011Z","repository":{"id":62567292,"uuid":"111997672","full_name":"krassowski/declarative-parser","owner":"krassowski","description":"Modern, declarative argument parser for Python 3.6+","archived":false,"fork":false,"pushed_at":"2018-04-09T11:00:40.000Z","size":39,"stargazers_count":36,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-01-08T21:56:14.858Z","etag":null,"topics":["argparse","argument-parser","argument-parsing","automagic","cli","command-line","command-line-parser","commandline-interface","declarative","declarative-parser","docstrings","option-parser","option-parsing"],"latest_commit_sha":null,"homepage":"https://pypi.python.org/pypi/declarative-parser","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krassowski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-25T11:16:05.000Z","updated_at":"2025-04-09T14:37:57.000Z","dependencies_parsed_at":"2022-11-03T16:30:39.464Z","dependency_job_id":null,"html_url":"https://github.com/krassowski/declarative-parser","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/krassowski/declarative-parser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krassowski%2Fdeclarative-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krassowski%2Fdeclarative-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krassowski%2Fdeclarative-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krassowski%2Fdeclarative-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krassowski","download_url":"https://codeload.github.com/krassowski/declarative-parser/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krassowski%2Fdeclarative-parser/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29953287,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-28T18:42:55.706Z","status":"ssl_error","status_checked_at":"2026-02-28T18:42:48.811Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["argparse","argument-parser","argument-parsing","automagic","cli","command-line","command-line-parser","commandline-interface","declarative","declarative-parser","docstrings","option-parser","option-parsing"],"created_at":"2024-11-15T11:23:03.107Z","updated_at":"2026-02-28T22:31:54.984Z","avatar_url":"https://github.com/krassowski.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Declarative Parser\n[![Build Status](https://travis-ci.org/krassowski/declarative-parser.svg?branch=master)](https://travis-ci.org/krassowski/declarative-parser) [![Code Climate](https://codeclimate.com/github/krassowski/declarative-parser/badges/gpa.svg)](https://codeclimate.com/github/krassowski/declarative-parser) [![Coverage Status](https://coveralls.io/repos/github/krassowski/declarative-parser/badge.svg)](https://coveralls.io/github/krassowski/declarative-parser) [![Documentation Status](https://readthedocs.org/projects/declarative-parser/badge/?version=latest)](http://declarative-parser.readthedocs.io/en/latest/?badge=latest)\n\nModern, declarative argument parser for Python 3.6+.\nPowerful like click, integrated like argparse, declarative as sqlalchemy. MIT licenced. [Documented on RTD](http://declarative-parser.readthedocs.io/en/latest/). Install with:\n\n```bash\npython3 -m pip install declarative_parser\n```\n\n\n### As simple as argparse\n\nIt's built on top of argparse - everything you already know stays valid!\n\n```python\nfrom declarative_parser import Parser, Argument\n\nclass MyParser(Parser):\n    square = Argument(help='display a square of a given number')\n\nparser = MyParser()\nargs = parser.parse_args()\nprint(args.square**2)\n```\n\n\n### Nested and Parallel\n\nEveryone knows about nested args. What about parallel groups?\n\n```python\nsupported_formats = ['png', 'jpeg', 'gif']\n\nclass InputOptions(Parser):\n    path = Argument(type=argparse.FileType('rb'), optional=False)\n    format = Argument(default='png', choices=supported_formats)\n\nclass OutputOptions(Parser):\n    format = Argument(default='jpeg', choices=supported_formats)\n    scale = Argument(type=int, default=100, help='Rescale image to %% of original size')\n\nclass ImageConverter(Parser):\n    description = 'This app converts images'\n\n    verbose = Argument(action='store_true')\n    input = InputOptions()\n    output = OutputOptions()\n\nparser = ImageConverter()\n\ncommands = '--verbose input image.png output --format gif --scale 50'.split()\n\nnamespace = parser.parse_args(commands)\n\nassert namespace.input.format == 'png'\nassert namespace.output.format == 'gif'\n```\n\n\n### Intelligent\n\nMake use of Python 3 type hints to reduce tedious task of parsers writing to two or three lines.\nPositional, keyword arguments, type hints, docstrings - everything can be meaningfully transformed into a parser.\nAnd if you decide to take control, just overwrite the automatically deduced arguments with an `Argument()` defined as a class variable.\n\n```python\nimport argparse\nfrom declarative_parser import Argument\nfrom declarative_parser.constructor_parser import ConstructorParser\n\nclass MyProgram:\n\n    database = Argument(\n        type=argparse.FileType('r'),\n        help='Path to file with the database'\n    )\n\n    def __init__(self, text: str, threshold: float=0.05, database=None):\n        \"\"\"My program does XYZ.\n\n        Arguments:\n          threshold: a floating-point value defining threshold, default 0.05\n          database: file object to the database if any\n        \"\"\"\n        print(text, threshold, None)\n\nparser = ConstructorParser(MyProgram)\n\noptions = parser.parse_args()\nprogram = parser.constructor(**vars(options))\n```\n\nAnd it works quite intuitively:\n\n```bash\n$ ./my_program.py test --threshold 0.6\ntest 0.6 None\n$ ./my_program.py test --threshold f\nusage: my_program.py [-h] [--database DATABASE] [--threshold THRESHOLD] text {} ...\nmy_program.py: error: argument --threshold: invalid float value: 'f'\n$ ./my_program.py --threshold 0.6\nusage: my_program.py [-h] [--database DATABASE] [--threshold THRESHOLD] text {} ...\nmy_program.py: error: the following arguments are required: text\n```\n\nThree docstring formats are supported: Google, NumPy and reStructuredText, with the default being Google.\n\nPS. It works with functions too; see the documentation of [FunctionParser](http://declarative-parser.readthedocs.io/en/latest/constructor_parser.html#declarative_parser.constructor_parser.FunctionParser).\n\n### Practical\n\nWhat if you only want to show licence of your program? or version? Is there a need to write a separate logic?\nDeclarativeParser gives you utility decorator: `@action` which utilizes the power of `argparse.Action`,\nleaving behind the otherwise necessary boilerplate code.\n\n```python\n__version__ = 2.0\n\nimport argparse\nfrom declarative_parser import action\nfrom declarative_parser.constructor_parser import ConstructorParser\n\nclass MyProgram:\n\n    def __init__(self, threshold: float=0.05):\n        \"\"\"My program does XYZ.\n\n        Arguments:\n          threshold: a floating-point value, default 0.05\n        \"\"\"\n        pass\n\n    @action\n    def version(options):\n       print(__version__)\n\nparser = ConstructorParser(MyProgram)\n\noptions = parser.parse_args()\nprogram = parser.constructor(**vars(options))\n```\n\nThe execution of an action will (by default) cause the program to exit immediately when finished.\n\nSee following run as example:\n\n```bash\n$ ./my_program.py --version\n2.0\n```\n\n\nSee more examples in [the documentation](http://declarative-parser.readthedocs.io/en/latest/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrassowski%2Fdeclarative-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrassowski%2Fdeclarative-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrassowski%2Fdeclarative-parser/lists"}