D
D
denislysenko2021-10-07 13:44:29
Python
denislysenko, 2021-10-07 13:44:29

How to escape an element on the command line (argpars module)?

Hello!
You need to write a command line utility to sort movies.
There is an argument -genres - this is a filter by genre, set by the user. may be multiple. for example, Comedy|Adventure or Comedy&Adventure. optional

That is, in the console you can enter:
python3 example_one.py -genres Comedy # and this command works in the console

But you need to be able to filter by several genres at once, for example,
python3 example_one.py -genres Romantic|Drama # this command does not work in the
python3 console example_one.py -genres Romantic&Drama # this command also doesn't work

here is what is written in the console when these two commands are executed:

zsh: command not found: Drama
Exception ignored in: <_io.TextIOWrapper name='' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe

here's what the add_argument method looks like:

parser.add_argument('-genres',
                    type=str,
                    help='filter by genre'
                    )


How to make it possible to pass several values ​​to this argument in the console via | or &
For example, if I write in the console:
python3 example_one.py -genres Romantic|Drama
!!! Then I need the value of the -genres argument to be "Romantic|Drama" , and then another function will already work with this line.
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-10-07
@denislysenko

Maybe it will be easier this way?
example.py -genres Romantic -genres Drama
argparse can do this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question