Skip to content

Commit

Permalink
Merge pull request #12 from vanyakosmos/i10
Browse files Browse the repository at this point in the history
treat args with the same name (but in different subparsers) as different values
  • Loading branch information
vanyakosmos committed Nov 1, 2019
2 parents 5e05276 + 5dd0e7b commit 6951366
Show file tree
Hide file tree
Showing 22 changed files with 1,082 additions and 399 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Versions follow [Semantic Versioning](https://semver.org) (`<major>.<minor>.<pat

- build parser based on function arguments
- build parser with sub-commands using multiple functions
- add constructor parameter for options


## 0.0.11
Expand Down
45 changes: 27 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# argser

[![PyPI version](https://badge.fury.io/py/argser.svg)](http://badge.fury.io/py/argser)
[![Downloads](https://pepy.tech/badge/argser)](https://pepy.tech/project/argser)
[![Build Status](https://github.com/vanyakosmos/argser/workflows/test-publish/badge.svg)](https://github.com/vanyakosmos/argser/actions?workflow=test-publish)
[![Coverage](https://codecov.io/gh/vanyakosmos/argser/branch/master/graph/badge.svg)](https://codecov.io/gh/vanyakosmos/argser)
[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://pypi.python.org/pypi/argser/)
[![Downloads](https://pepy.tech/badge/argser)](https://pepy.tech/project/argser)
[![Docs](https://readthedocs.org/projects/argser/badge/?version=stable)](https://argser.readthedocs.io/en/stable/)

[GitHub](https://github.com/vanyakosmos/argser) |
[PyPI](https://pypi.org/project/argser/) |
[Docs](https://argser.readthedocs.io/en/latest) |
[Examples](https://argser.readthedocs.io/en/latest/examples.html) |
[Docs](https://argser.readthedocs.io/en/stable) |
[Examples](https://argser.readthedocs.io/en/stable/examples.html) |
[Installation](https://argser.readthedocs.io/en/stable/installation.html) |
[Changelog](CHANGELOG.md)

Arguments parsing without boilerplate.
Expand All @@ -35,6 +36,12 @@ pip install argser[all]
```


## Notes for examples

If second parameter of `parse_args` is string (as in almost all examples) then it will be parsed,
otherwise arguments to parse will be taken from command line.


## Simple example

```python
Expand All @@ -44,7 +51,7 @@ class Args:
a = 'a'
foo = 1
bar: bool

bar_baz = 42, "bar_baz help"

args = parse_args(Args, show=True)
```
Expand All @@ -61,6 +68,7 @@ parser.add_argument('--foo', '-f', dest='foo', type=int, default=1, help="int, d
parser.add_argument('--bar', '-b', dest='bar', action='store_true', help="bool, default: None")
parser.add_argument('--no-bar', '--no-b', dest='bar', action='store_false')
parser.set_defaults(bar=None)
parser.add_argument('--bar-baz', dest='bar_baz', default=42, help="int, default: 42. bar_baz help")

args = parser.parse_args()
print(args)
Expand All @@ -69,20 +77,20 @@ print(args)

```text
❯ python playground.py -a "aaa bbb" -f 100500 --no-b
>> Args(bar=False, a='aaa bbb', foo=100500)
>>> Args(bar=False, a='aaa bbb', foo=100500, bar_baz=42)
```

```text
❯ python playground.py -h
usage: playground.py [-h] [--bar] [--no-bar] [-a [A]] [--foo [FOO]]
usage: playground.py [-h] [--bar] [--no-bar] [-a A] [--foo F] [--bar-baz B]
optional arguments:
-h, --help show this help message and exit
--bar, -b bool, default: None.
--no-bar, --no-b
-a [A] str, default: 'a'.
--foo [FOO], -f [FOO]
int, default: 1.
-h, --help show this help message and exit
--bar, -b bool, default: None
--no-bar, --no-b
-a A str, default: 'a'
--foo F, -f F int, default: 1
--bar-baz B, --bb B int, default: 42. bar_baz help
```


Expand All @@ -102,19 +110,20 @@ assert argser.call(foo, '1 2 -c 3.4') == ['1', 2, 3.4]

```python
from argser import parse_args, sub_command

class SubArgs:
d = 1
e = '2'

class Args:
a: bool
b = []
c = 5

class SubArgs:
d = 1
e = '2'
sub = sub_command(SubArgs, help='help message for sub-command')

args = parse_args(Args, '-a -c 10', parser_help='help message for root parser')
args = parse_args(Args, '-a -b a b -c 10', parser_help='help message for root parser')
assert args.a is True
assert args.b == ['a', 'b']
assert args.c == 10
assert args.sub is None

Expand Down
2 changes: 1 addition & 1 deletion argser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__version__ = "0.0.11"

from argser.consts import FALSE_VALUES, TRUE_VALUES
from argser.display import make_table, stringify
from argser.display import stringify, print_args
from argser.fields import Arg, Opt
from argser.parse_func import SubCommands, call
from argser.parser import make_parser, parse_args, populate_holder, sub_command
Expand Down
1 change: 0 additions & 1 deletion argser/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
TRUE_VALUES = {'1', 'true', 't', 'okay', 'ok', 'affirmative', 'yes', 'y', 'totally'}
FALSE_VALUES = {'0', 'false', 'f', 'no', 'n', 'nope', 'nah'}
SUB_COMMAND_MARK = '__sub_command'
SUB_COMMAND_DEST_FMT = '__{name}_sub_command__'
16 changes: 7 additions & 9 deletions argser/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def stringify(args: Args, shorten=False):
def pair(x):
k, v = x
if shorten:
v = repr(v)
v = textwrap.shorten(v, width=20, placeholder='...')
v = textwrap.shorten(str(v), width=20, placeholder='...')
v = repr(v)
return f"{k}={v}"

pairs = ', '.join(map(pair, args.__dict__.items()))
Expand All @@ -27,9 +27,9 @@ def pair(x):
if v is None:
v = colors.red('-')
else:
v = repr(v)
if shorten:
v = textwrap.shorten(v, width=20, placeholder='...')
v = textwrap.shorten(str(v), width=20, placeholder='...')
v = repr(v)
return f"{colors.green(k)}={v}"

pairs = ', '.join(map(pair, args.__dict__.items()))
Expand Down Expand Up @@ -148,8 +148,8 @@ def print_args(args: Args, variant=None, print_fn=None, colorize=True, shorten=F
:param args: some object with attributes
:param variant:
if truthy value - print arguments in one line
if 'table' - print arguments as table
otherwise print arguments in one line
:param print_fn:
:param colorize: add colors to the help message and arguments printing
:param shorten: shorten long text (eg long default value)
Expand All @@ -161,9 +161,7 @@ def print_args(args: Args, variant=None, print_fn=None, colorize=True, shorten=F
if variant == 'table':
s = make_table(args, colorize=colorize, shorten=shorten, **kwargs)
else:
if colorize:
s = stringify_colored(args, shorten)
else:
s = stringify(args, shorten)
to_str = stringify_colored if colorize else stringify
s = to_str(args, shorten)
print_fn = print_fn or print
print_fn(s)

0 comments on commit 6951366

Please sign in to comment.