Replace getopt CLI option parsing with a cleaner argparse parsing

master
Guillaume Seguin 2013-05-16 12:21:37 +02:00
parent f63dfe9981
commit fb3e4068c0
1 changed files with 14 additions and 18 deletions

View File

@ -20,7 +20,7 @@ import glob, os, time, datetime
import sys, subprocess import sys, subprocess
import math, codecs import math, codecs
from math import sqrt from math import sqrt
import getopt import argparse
import printcore import printcore
from printrun.printrun_utils import install_locale from printrun.printrun_utils import install_locale
@ -1208,25 +1208,21 @@ class pronsole(cmd.Cmd):
self.log("home xyze - homes all axes and zeroes the extruder (Using G28 and G92)") self.log("home xyze - homes all axes and zeroes the extruder (Using G28 and G92)")
def parse_cmdline(self, args): def parse_cmdline(self, args):
opts, args = getopt.getopt(args, "c:e:hw", ["conf = ", "config = ", "help"]) parser = argparse.ArgumentParser(description = 'Printrun 3D printer interface')
for o, a in opts: parser.add_argument('-c','--conf','--config', help = _("load this file on startup instead of .pronsolerc ; you may chain config files, if so settings auto-save will use the last specified file"), action = "append", default = [])
if o in ("-c", "--conf", "--config"): parser.add_argument('-e','--execute', help = _("executes command after configuration/.pronsolerc is loaded ; macros/settings from these commands are not autosaved"), action = "append", default = [])
self.load_rc(a) parser.add_argument('filename', nargs='?', help = _("file to load"))
elif o in ("-h", "--help"): args = parser.parse_args()
self.log("Usage: "+sys.argv[0]+' [-c filename [-c filename2 ... ] ] [-e "command" ...]') for config in args.conf:
self.log(" -c | --conf | --config - override startup .pronsolerc file") self.load_rc(config)
self.log(" may chain config files, settings auto-save will go into last file in the chain")
self.log(' -e <command> - executes command after configuration/.pronsolerc is loaded')
self.log(" macros/settings from these commands are not autosaved")
sys.exit()
if not self.rc_loaded: if not self.rc_loaded:
self.load_default_rc() self.load_default_rc()
for o, a in opts: self.processing_args = True
if o == "-e": for command in args.execute:
self.processing_args = True self.onecmd(command)
self.onecmd(a) self.processing_args = False
self.processing_args = False if args.filename:
self.do_load(args.filename)
# We replace this function, defined in cmd.py . # We replace this function, defined in cmd.py .
# It's default behavior with reagrds to Ctr-C # It's default behavior with reagrds to Ctr-C