Name | typed-args JSON |
Version |
0.6.5
JSON |
| download |
home_page | None |
Summary | Parse command line arguments by defining dataclasses |
upload_time | 2024-09-23 17:05:34 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.6 |
license | BSD 3-Clause License Copyright (c) 2019, SunDoge All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
keywords |
argparse
cli
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# typed-args
[](https://github.com/SunDoge/typed-args/actions/workflows/python-package.yml)
[](https://pypi.org/project/typed-args/)
This project is inspired by [TeXitoi/structopt](https://github.com/TeXitoi/structopt).
## Introduction
`typed-args` is a Python package for creating command line interfaces with type annotations.
The program defines what arguments it requires, and `typed-args` will figure out how to parse them out of `sys.argv`.
`typed-args` use standard python library `argparse` and `dataclasses` so no need to install any dependencies after Python 3.6.
Its API is very similar to `argparse`.
What does it look like? Here is an [example](https://docs.python.org/3/library/argparse.html#example) from `argparse` docs and is rewritten with `typed-args`:
```python
import typed_args as ta
from typing import List, Callable
@ta.argument_parser(
description='Process some integers.'
)
class Args(ta.TypedArgs):
integers: List[int] = ta.add_argument(
metavar='N', type=int, nargs='+',
help='an integer for the accumulator'
)
accumulate: Callable[[List[int]], int] = ta.add_argument(
'--sum',
action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)'
)
args = Args.parse_args()
print(args.accumulate(args.integers))
```
Assuming the above Python code is saved into a file called `prog.py`, it can be run at the command line and it provides useful help messages:
```text
$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]
Process some integers.
positional arguments:
N an integer for the accumulator
optional arguments:
-h, --help show this help message and exit
--sum sum the integers (default: find the max)
```
When run with the appropriate arguments, it prints either the sum or the max of the command-line integers:
```text
$ python prog.py 1 2 3 4
4
$ python prog.py 1 2 3 4 --sum
10
```
If invalid arguments are passed in, an error will be displayed:
```text
$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
```
## Installation
From pypi
```bash
pip install typed-args
```
If you want to use it on python 3.5 and 3.6 please install `dataclasses`:
```bash
pip install dataclasses
```
## Core Functionality
Check [_test_v0_6.py](https://github.com/SunDoge/typed-args/blob/master/_test_v0_6.py) for `add_argument_group` and `add_subparsers`.
### Create a parser
`argparse`
```python
import argparse
parser = argparse.ArgumentParser(prog='ProgramName')
```
`typed-args`
```python
import typed_args as ta
@ta.argument_parser(prog='ProgramName')
class Args(ta.TypedArgs):
pass
```
### Add arguments
`argparse`
```python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('filename') # positional argument
parser.add_argument('-c', '--count') # option that takes a value
parser.add_argument('-v', '--verbose',
action='store_true') # on/off flag
```
`typed-args`
```python
import typed_args as ta
@ta.argument_parser()
class Args(ta.TypedArgs):
filename: str = ta.add_argument() # positional argument, use the attribute name automatically
count: str = ta.add_argument('-c', '--count') # option that takes a value, also can be annotated as Optional[str]
verbose: bool = ta.add_argument('-v', '--verbose',
action='store_true') # on/off flag
```
### Parse args
`argparse`
```python
args = parser.parse_args()
print(args.filename, args.count, args.verbose)
```
`typed-args`
```python
args = Args.parse_args()
print(args.filename, args.count, args.verbose)
```
Raw data
{
"_id": null,
"home_page": null,
"name": "typed-args",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "argparse, cli",
"author": null,
"author_email": "SunDoge <triplez0@outlook.com>",
"download_url": "https://files.pythonhosted.org/packages/9e/3e/9200f084be31d5068de9002aa456526ec4a2a9bf83d40da4e82439bc240c/typed_args-0.6.5.tar.gz",
"platform": null,
"description": "# typed-args\n\n[](https://github.com/SunDoge/typed-args/actions/workflows/python-package.yml)\n[](https://pypi.org/project/typed-args/)\n\nThis project is inspired by [TeXitoi/structopt](https://github.com/TeXitoi/structopt).\n\n## Introduction\n\n`typed-args` is a Python package for creating command line interfaces with type annotations. \nThe program defines what arguments it requires, and `typed-args` will figure out how to parse them out of `sys.argv`. \n`typed-args` use standard python library `argparse` and `dataclasses` so no need to install any dependencies after Python 3.6. \nIts API is very similar to `argparse`. \n\n\nWhat does it look like? Here is an [example](https://docs.python.org/3/library/argparse.html#example) from `argparse` docs and is rewritten with `typed-args`:\n\n```python\nimport typed_args as ta\nfrom typing import List, Callable\n\n\n@ta.argument_parser(\n description='Process some integers.'\n)\nclass Args(ta.TypedArgs):\n integers: List[int] = ta.add_argument(\n metavar='N', type=int, nargs='+',\n help='an integer for the accumulator'\n )\n accumulate: Callable[[List[int]], int] = ta.add_argument(\n '--sum',\n action='store_const',\n const=sum, default=max,\n help='sum the integers (default: find the max)'\n )\n\n\nargs = Args.parse_args()\nprint(args.accumulate(args.integers))\n```\n\nAssuming the above Python code is saved into a file called `prog.py`, it can be run at the command line and it provides useful help messages:\n\n```text\n$ python prog.py -h\nusage: prog.py [-h] [--sum] N [N ...]\n\nProcess some integers.\n\npositional arguments:\n N an integer for the accumulator\n\noptional arguments:\n -h, --help show this help message and exit\n --sum sum the integers (default: find the max)\n```\n\nWhen run with the appropriate arguments, it prints either the sum or the max of the command-line integers:\n\n```text\n$ python prog.py 1 2 3 4\n4\n\n$ python prog.py 1 2 3 4 --sum\n10\n```\n\nIf invalid arguments are passed in, an error will be displayed:\n\n```text\n$ python prog.py a b c\nusage: prog.py [-h] [--sum] N [N ...]\nprog.py: error: argument N: invalid int value: 'a'\n```\n\n\n\n## Installation\n\nFrom pypi\n\n```bash\npip install typed-args\n```\n\nIf you want to use it on python 3.5 and 3.6 please install `dataclasses`:\n\n```bash\npip install dataclasses\n```\n\n## Core Functionality\n\nCheck [_test_v0_6.py](https://github.com/SunDoge/typed-args/blob/master/_test_v0_6.py) for `add_argument_group` and `add_subparsers`.\n\n\n### Create a parser\n\n`argparse`\n\n```python\nimport argparse\nparser = argparse.ArgumentParser(prog='ProgramName')\n```\n\n`typed-args`\n\n```python\nimport typed_args as ta\n\n@ta.argument_parser(prog='ProgramName')\nclass Args(ta.TypedArgs):\n pass\n```\n\n### Add arguments\n\n`argparse`\n\n```python\nimport argparse\nparser = argparse.ArgumentParser()\nparser.add_argument('filename') # positional argument\nparser.add_argument('-c', '--count') # option that takes a value\nparser.add_argument('-v', '--verbose',\n action='store_true') # on/off flag\n```\n\n`typed-args`\n\n```python\nimport typed_args as ta\n\n@ta.argument_parser()\nclass Args(ta.TypedArgs):\n filename: str = ta.add_argument() # positional argument, use the attribute name automatically\n count: str = ta.add_argument('-c', '--count') # option that takes a value, also can be annotated as Optional[str]\n verbose: bool = ta.add_argument('-v', '--verbose', \n action='store_true') # on/off flag\n```\n\n### Parse args\n\n`argparse`\n\n```python\nargs = parser.parse_args()\nprint(args.filename, args.count, args.verbose)\n```\n\n`typed-args`\n\n```python\nargs = Args.parse_args()\nprint(args.filename, args.count, args.verbose)\n```\n\n\n",
"bugtrack_url": null,
"license": "BSD 3-Clause License Copyright (c) 2019, SunDoge All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.",
"summary": "Parse command line arguments by defining dataclasses",
"version": "0.6.5",
"project_urls": {
"Homepage": "https://github.com/SunDoge/typed-args",
"Repository": "https://github.com/SunDoge/typed-args"
},
"split_keywords": [
"argparse",
" cli"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3c7a1a55d964cb1b43e48d3cfce278efe3e55ea793c5e9d4e92a5ed5feca143c",
"md5": "40fddae88fa6fad64590c1855d025da8",
"sha256": "ee7329e6dccd97140205e0ae1119b268f213045655f42d5b5b03c4645e66aa68"
},
"downloads": -1,
"filename": "typed_args-0.6.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "40fddae88fa6fad64590c1855d025da8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 8789,
"upload_time": "2024-09-23T17:05:33",
"upload_time_iso_8601": "2024-09-23T17:05:33.014135Z",
"url": "https://files.pythonhosted.org/packages/3c/7a/1a55d964cb1b43e48d3cfce278efe3e55ea793c5e9d4e92a5ed5feca143c/typed_args-0.6.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9e3e9200f084be31d5068de9002aa456526ec4a2a9bf83d40da4e82439bc240c",
"md5": "01e2eece94983af0c9a1e91af8720696",
"sha256": "4f7194c7c98c16b78a52b1a467caae2cfffcff402e2ad4b90f57f77a92f018fe"
},
"downloads": -1,
"filename": "typed_args-0.6.5.tar.gz",
"has_sig": false,
"md5_digest": "01e2eece94983af0c9a1e91af8720696",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 15297,
"upload_time": "2024-09-23T17:05:34",
"upload_time_iso_8601": "2024-09-23T17:05:34.163935Z",
"url": "https://files.pythonhosted.org/packages/9e/3e/9200f084be31d5068de9002aa456526ec4a2a9bf83d40da4e82439bc240c/typed_args-0.6.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-23 17:05:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "SunDoge",
"github_project": "typed-args",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "typed-args"
}