Re: Python Command Line Arguments
On Saturday, April 15, 2017 at 6:45:51 PM UTC+1, Grant Edwards wrote: > On 2017-04-14, Bernd Nawothnig wrote: > > > He should switch to argparse in any case because getopt is no longer > > supported and does only receive bugfixes. > > In my book, "receiving bug fixes" means it's still supported. > > -- > Grant Just to reinforce what I said before, optparse is deprecated, not getopt. Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Command Line Arguments
On Friday, April 14, 2017 at 2:44:09 PM UTC+1, Bernd Nawothnig wrote: > On 2017-04-13, Jason Friedman wrote: > >> I have this code which I got from https://www.tutorialspoint. > >> com/python/python_command_line_arguments.htm The example works fine but > >> when I modify it to what I need, it only half works. The problem is the > >> try/except. If you don't specify an input/output, they are blank at the end > >> but it shouldn't be. > >> > >> import getopt > >> import sys > > > > I am guessing you are wanting to parse command-line arguments rather than > > particularly wanting to use the getopt module. > > If I am correct you might want to spend your time instead learning the > > argparse module: > > https://docs.python.org/3/library/argparse.html > > https://docs.python.org/3/howto/argparse.html > > He should switch to argparse in any case because getopt is no longer > supported and does only receive bugfixes. > > Bernd > > -- > Die Antisemiten vergeben es den Juden nicht, dass die Juden ‘Geist’ > haben – und Geld. Die Antisemiten – ein Name der > ‘Schlechtweggekommenenen’ [Friedrich Nietzsche] optparse is deprecated, not getopt. Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Command Line Arguments
On 2017-04-14, Bernd Nawothnigwrote: > He should switch to argparse in any case because getopt is no longer > supported and does only receive bugfixes. In my book, "receiving bug fixes" means it's still supported. -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Command Line Arguments
On 2017-04-13, Jason Friedman wrote: >> I have this code which I got from https://www.tutorialspoint. >> com/python/python_command_line_arguments.htm The example works fine but >> when I modify it to what I need, it only half works. The problem is the >> try/except. If you don't specify an input/output, they are blank at the end >> but it shouldn't be. >> >> import getopt >> import sys > > I am guessing you are wanting to parse command-line arguments rather than > particularly wanting to use the getopt module. > If I am correct you might want to spend your time instead learning the > argparse module: > https://docs.python.org/3/library/argparse.html > https://docs.python.org/3/howto/argparse.html He should switch to argparse in any case because getopt is no longer supported and does only receive bugfixes. Bernd -- Die Antisemiten vergeben es den Juden nicht, dass die Juden ‘Geist’ haben – und Geld. Die Antisemiten – ein Name der ‘Schlechtweggekommenenen’ [Friedrich Nietzsche] -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Command Line Arguments
On Thursday, April 13, 2017 at 12:38:48 PM UTC+10, MRAB wrote: > On 2017-04-13 02:59, ian.steg...@gmail.com wrote: > > I have this code which I got from > > https://www.tutorialspoint.com/python/python_command_line_arguments.htm The > > example works fine but when I modify it to what I need, it only half works. > > The problem is the try/except. If you don't specify an input/output, they > > are blank at the end but it shouldn't be. > > > > > > import getopt > > import sys > > > > def main(argv): > > inputfile = '' > > outputfile = '' > > try: > >opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) > > except getopt.GetoptError: > >inputfile = 'Input' > >outputfile = 'Output' > > if inputfile == '': > >for opt, arg in opts: > > if opt == '-h': > > print ('Usage: Encrypt.py -i -o ') > > sys.exit() > > elif opt in ("-i", "--ifile"): > > inputfile = arg > > elif opt in ("-o", "--ofile"): > > outputfile = arg > > else: > >'' > > > > print 'In: ' + inputfile > > print 'Out: ' + outputfile > > > > if __name__ == "__main__": > > main(sys.argv[1:]) > > > You'll get the GetoptError exception if an option that requires an > argument doesn't have one. That's not the same as omitting the option > entirely. > > For example: > > # No -i option. > foo > > # Option -i present but without its required argument. > foo -i WOW. Thanks for that. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Command Line Arguments
On 2017-04-13 02:59, ian.steg...@gmail.com wrote: I have this code which I got from https://www.tutorialspoint.com/python/python_command_line_arguments.htm The example works fine but when I modify it to what I need, it only half works. The problem is the try/except. If you don't specify an input/output, they are blank at the end but it shouldn't be. import getopt import sys def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: inputfile = 'Input' outputfile = 'Output' if inputfile == '': for opt, arg in opts: if opt == '-h': print ('Usage: Encrypt.py -i -o ') sys.exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg else: '' print 'In: ' + inputfile print 'Out: ' + outputfile if __name__ == "__main__": main(sys.argv[1:]) You'll get the GetoptError exception if an option that requires an argument doesn't have one. That's not the same as omitting the option entirely. For example: # No -i option. foo # Option -i present but without its required argument. foo -i -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Command Line Arguments
> > I have this code which I got from https://www.tutorialspoint. > com/python/python_command_line_arguments.htm The example works fine but > when I modify it to what I need, it only half works. The problem is the > try/except. If you don't specify an input/output, they are blank at the end > but it shouldn't be. > > import getopt > import sys > Hello Ian, I am guessing you are wanting to parse command-line arguments rather than particularly wanting to use the getopt module. If I am correct you might want to spend your time instead learning the argparse module: https://docs.python.org/3/library/argparse.html https://docs.python.org/3/howto/argparse.html -- https://mail.python.org/mailman/listinfo/python-list
Python Command Line Arguments
I have this code which I got from https://www.tutorialspoint.com/python/python_command_line_arguments.htm The example works fine but when I modify it to what I need, it only half works. The problem is the try/except. If you don't specify an input/output, they are blank at the end but it shouldn't be. import getopt import sys def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: inputfile = 'Input' outputfile = 'Output' if inputfile == '': for opt, arg in opts: if opt == '-h': print ('Usage: Encrypt.py -i -o ') sys.exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg else: '' print 'In: ' + inputfile print 'Out: ' + outputfile if __name__ == "__main__": main(sys.argv[1:]) -- https://mail.python.org/mailman/listinfo/python-list