I am trying to specify an optional argument that stdin takes. This will be mainly used for data transfer in my program, therefore someprog that outputs | python my_prog.
I followed the argparse documentation and I read a lot of questions / answers about this in Stackoverflow, but none of them seem to work for me.
Here is what I originally had:
parser = argparse.ArgumentParser(description='Upgrade Instance.')
parser.add_argument('--app', '-a', dest='app', action='store', required=True)
parser.add_argument('--version', '-v', dest='version', action='store', default='', required=False)
parser.add_argument('--config', '-c', dest='config', action='store', default = '', required=False)
args = parser.parse_args()
Now what I want to do is let the user go through versionwith the channel, rather than transmit it.
I added parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin)to the beginning, but that makes it a positional argument. How is this possible? I thought that nargs=?made it optional.
I need this to be an optional argument. So I changed it to:
parser.add_argument('--infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
, stdin, , . default=sys.stdin - , :
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
. args, : Namespace(app='app', config='', g=False, hosts='03.app', infile=None, version='').
, , . , , .
, ?