1
votes

I have a script that uses some arguments and some stdin data.

For checking arguments I use argparse.ArgumentParser

Is it possible to check if any stdin data is given? Something like that:

parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin, required=True)

but this example gives this error:

TypeError: 'required' is an invalid argument for positionals

3

3 Answers

1
votes

No. It wont't read from whatever file you pass it, be it given on the command line, or stdin. You will get an open file handle, with not even a single byte/char consumed.

Simply read the data yourself, for instance with data = args.infile.read() (assuming args is the result of parsing`).

You can then test if it is empty, with a simple if not data:...

But usually, if you expect data in a specific format, the best is to simply try to parse it, and raise an error if you fail. Either empty data is invalid (json for instance), or it is valid but then it should be an acceptable input.

(as for the error, required only tells whether some option must be given on the command line or not, for --options and -o options. Positionals are always required unless you change their numbers with nargs).

1
votes

The error is just because of the required=True parameter; and the message tells you what is wrong. It should be:

parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin)

By 'calling' this infile, as opposed to '--infile', you've created a positional argument. argparse itself determines whether it is required or not. With nargs='?' it can't be required. It's by definition optional (but not an optionals argument :) ).

The FileType type lets you name a file (or '-') in the commandline. It will open it (stdin is already open) and assign it to the args.infile attribute. It does nothing more.

So after parsing, using args.infile gives you access to this open file, which you can read as needed (and optionally close if not stdin).

So this is a convenient way of letting your users specify which file should be opened for use in your code. It was intended for simple scripts that read one file, do something, and write to another.

But if all you are looking at is stdin, there isn't any point in using this type. sys.stdin is always available for reading. And there isn't any way of making the parser read stdin. It parses sys.argv which comes from the commandline.

There is an @ prefix file feature that tells the parser to read commandline strings from a file. It parses the file and splices the values into sys.argv. See the argparse docs.

0
votes

From the docs The add_argument() method

required - Whether or not the command-line option may be omitted (optionals only).

The required keyword is only used for options (e.g., -f or --foo) not for positional arguments. Just take it out.

parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
    default=sys.stdin)

When parsed infile will either be a string or the sys.stdin file object. You would need to read that file to see if there is anything in it. Reading can be risky... you may block forever. But that just means that the user didn't follow instructions.