python / cpython

The Python programming language
https://www.python.org
Other
62.6k stars 30.04k forks source link

Argparse: wildcards processing #86386

Open 2411d3b6-f96a-4464-9e72-e121c9e9f1ca opened 3 years ago

2411d3b6-f96a-4464-9e72-e121c9e9f1ca commented 3 years ago
BPO 42220
Nosy @andrewerf

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['3.7', '3.8', '3.9', '3.10', 'type-feature', 'library'] title = 'Argparse: wildcards processing' updated_at = user = 'https://github.com/andrewerf' ``` bugs.python.org fields: ```python activity = actor = 'Andrew' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'Andrew' dependencies = [] files = [] hgrepos = [] issue_num = 42220 keywords = [] message_count = 1.0 messages = ['380076'] nosy_count = 1.0 nosy_names = ['Andrew'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue42220' versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10'] ```

2411d3b6-f96a-4464-9e72-e121c9e9f1ca commented 3 years ago

It's known that Linux and Windows shells process wildcards like '*' and '?' differently. Linux shell usually expands wildcards so the application gets list of files. In contrast, Windows passes the wildcards to application. So in cross-platform applications we have to expand wildcards manually only for windows. It may not be beautiful since it's usually not part of program's logic and seems kinda of arguments processing. Could this problem be solved via adding optional functionality to argparse library? For instance some option 'expand_wildcards' in ArgumentParser constructor with default value False (in order to not break compatibility)?

serhiy-storchaka commented 2 days ago

You can simply expand wildcards in sys.argv:

argv = [x for y in sys.argv for x in (glob.glob(y) or [y])]
parser.parse_args(argv)

This is what the Unix shell does.

Or you can process only arguments for which this makes sense, after parsing them with argparse. In any case, only you know what arguments should be processed, so you should write some code. Specifying an option for argparse is also writing a code, but this is less flexible.