Support setting a postfix for cli
RowanLeeder opened this issue · comments
- I have marked all applicable categories:
- documentation request (i.e. "X is missing from the documentation." If instead I want to ask "how to use X?" I understand [StackOverflow#tqdm] is more appropriate)
- new feature request
- I have visited the [source website], and in particular
read the [known issues] - I have searched through the [issue tracker] for duplicates
- I have mentioned version numbers, operating system and
environment, where applicable:
It does not appear to be possible to pass a postfix argument from cli.
As of version 4.66.3 passing any value to --postfix
results in an error.
Eg: tqdm --log DEBUG --postfix 'foo' < /dev/null
:
DEBUG:cli:188:[('ascii', 'bool or str'), ('bar_format', 'str'), ('buf_size', 'int'), ('bytes', 'bool'), ('colour', 'str'), ('comppath', 'str'), ('delay', 'float'), ('delim', 'chr'), ('desc', 'str'), ('disable', 'bool'), ('dynamic_ncols', 'bool'), ('initial', 'int or float'), ('leave', 'bool'), ('lock_args', 'tuple'), ('log', 'str'), ('manpath', 'str'), ('maxinterval', 'float'), ('mininterval', 'float'), ('miniters', 'int or float'), ('ncols', 'int'), ('nrows', 'int'), ('null', 'bool'), ('position', 'int'), ('postfix', 'dict or *'), ('smoothing', 'float'), ('tee', 'bool'), ('total', 'int or float'), ('unit', 'str'), ('unit_divisor', 'float'), ('unit_scale', 'bool or int or float'), ('update', 'bool'), ('update_to', 'bool'), ('write_bytes', 'bool')]
DEBUG:cli:218:{'log': 'DEBUG', 'postfix': 'foo'}
DEBUG:cli:17:('foo', 'dict or *')
DEBUG:cli:17:('foo', 'dict')
DEBUG:cli:17:('foo', '*')
Error:
Usage:
tqdm [--help | options]
Traceback (most recent call last):
File "/home/local/user/.local/bin/tqdm", line 8, in <module>
sys.exit(main())
^^^^^^
File "/home/local/user/.local/lib/python3.11/site-packages/tqdm/cli.py", line 226, in main
tqdm_args[o] = cast(v, opt_types[o])
^^^^^^^^^^^^^^^^^^^^^
File "/home/local/user/.local/lib/python3.11/site-packages/tqdm/cli.py", line 24, in cast
raise TqdmTypeError(f"{val} : {typ}")
tqdm.std.TqdmTypeError: foo : dict or *
Postfix is a dict
/*
. cli.py:cast only supports bool
, chr
, str
, int
and float
.
lock_args
is a tuple
, so it is probably also unsupported.
Some suggestions would be:
- Handle dicts as JSON:
--postfix '{"foo": "1", "bar": "2"}'
etc. - Handle dicts by supporting multi-value args in the parser:
--postfix foo 1 --postfix bar 2
etc.
Aside, up to 4.66.2 it was technically possible to pass in a postfix. This relied on the vulnerability that was closed in 4.66.3 (see b53348c), so I wouldn't call this a bug.
Eg: tqdm --log DEBUG --postfix '", log=" foo' < /dev/null
.
DEBUG:cli:177:[('ascii', 'bool or str'), ('bar_format', 'str'), ('buf_size', 'int'), ('bytes', 'bool'), ('colour', 'str'), ('comppath', 'str'), ('delay', 'float'), ('delim', 'chr'), ('desc', 'str'), ('disable', 'bool'), ('dynamic_ncols', 'bool'), ('initial', 'int or float'), ('leave', 'bool'), ('lock_args', 'tuple'), ('log', 'str'), ('manpath', 'str'), ('maxinterval', 'float'), ('mininterval', 'float'), ('miniters', 'int or float'), ('ncols', 'int'), ('nrows', 'int'), ('null', 'bool'), ('position', 'int'), ('postfix', 'dict or *'), ('smoothing', 'float'), ('tee', 'bool'), ('total', 'int or float'), ('unit', 'str'), ('unit_divisor', 'float'), ('unit_scale', 'bool or int or float'), ('update', 'bool'), ('update_to', 'bool'), ('write_bytes', 'bool')]
DEBUG:cli:207:{'log': 'DEBUG', 'postfix': '", log=" foo'}
DEBUG:cli:17:('", log=" foo', 'dict or *')
DEBUG:cli:17:('", log=" foo', 'dict')
DEBUG:cli:218:args:{'file': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, 'postfix': {'log': ' foo'}}
DEBUG:cli:282:{'file': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, 'postfix': {'log': ' foo'}}
0it [00:00, ?it/s, log=foo]