Remove the analyzed parameters and their values ​​from sys.argv

I am trying to use optparse (for parsing command line options for my script) and fileinput (to be able to provide data through a pipe or file).

import optparse, fileinput parser = OptionParser() parser.add_option("-v", action="store_true", dest="verbose") (options, args) = parser.parse_args() for line in fileinput.input: process(line) 

However, fileinput tries to use the -v option as well as the file name, resulting in "There is no such file or directory error." So either I need to do fileinput args, or remove the parsed parameters from sys.argv, but I don't know how to do it. Any pointers?

+4
source share
1 answer

In the documentation:

To specify an alternate list of file names, pass it as the first argument to input() . A single file name is also allowed.

So, you can just go into the remaining args that you get from optparse .

+6
source

Source: https://habr.com/ru/post/1402071/


All Articles