Quoting Chad Versace (2017-03-28 09:37:56)
> On Sat 25 Mar 2017, Dylan Baker wrote:
> >
> > This is just a suggestion, feel free to ignore
> >
> > I forgot to do something here, but this is one case where using
> > argparse.FileType might actually be the right thing to do.
> >
> > it would like like:
> > p.add_argument('-o', '--output', type=argparse.FileType('wb'),
> > default=sys.stdout, ...)
> >
> > Then you could drop the hunk above and the 'with open(...)' in the main
> > function. It's also more portable.
>
> I just tried the suggestion, and it behaves badly IMO for output files. It's
> probably fine for input files. The problem is that ArgumentParser creates the
> file as soon as it parses the -o option, even when there is a usage error or
> the --help option is given.
>
> Example 1:
> $ ./gen_bits_header.py -o bad-output1 --bad-option
> usage: gen_bits_header.py [-h] [-o OUTPUT] [--cpp-guard CPP_GUARD]
> XML_SOURCE [XML_SOURCE ...]
> gen_bits_header.py: error: too few arguments
> $ echo $?
> 2
> $ ls
> ... bad-output1
>
> Example 2:
> $ ./gen_bits_header.py -o bad-output2 --help
> usage: gen_bits_header.py [-h] [-o OUTPUT] [--cpp-guard CPP_GUARD]
> XML_SOURCE [XML_SOURCE ...]
> $ echo $?
> 0
> $ ls
> ... bad-output2Yup, there are some painful corners to argparse.FileType that make me almost never use it, which is why it was just a suggestion :) Dylan
signature.asc
Description: signature
_______________________________________________ mesa-dev mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/mesa-dev
