From c89d86a779be070e1b4e6572c63bef01247d57ac Mon Sep 17 00:00:00 2001 From: f97ada87 Date: Sun, 24 Sep 2017 07:42:15 +1000 Subject: [PATCH] use getopt parser for command-line arguments --- src/bitmessagemain.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index b015d5bf..f5793ba5 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -29,6 +29,7 @@ from struct import pack from subprocess import call from time import sleep from random import randint +import getopt from api import MySimpleXMLRPCRequestHandler, StoppableXMLRPCServer from helper_startup import isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections @@ -199,11 +200,24 @@ class Main: def start(self, daemon=False): _fixSocket() - shared.daemon = daemon + try: + opts, args = getopt.getopt(sys.argv[1:], "hcd", + ["help", "curses", "daemon"]) - # get curses flag - if '-c' in sys.argv: - state.curses = True + except getopt.GetoptError: + self.usage() + sys.exit(2) + + for opt, arg in opts: + if opt in ("-h", "--help"): + self.usage() + sys.exit() + elif opt in ("-d", "--daemon"): + daemon = True + elif opt in ("-c", "--curses"): + state.curses = True + + shared.daemon = daemon # is the application already running? If yes then exit. shared.thisapp = singleinstance("", daemon) @@ -362,6 +376,17 @@ class Main: signal.signal(signal.SIGTERM, helper_generic.signal_handler) # signal.signal(signal.SIGINT, signal.SIG_DFL) + def usage(self): + print 'Usage: ' + sys.argv[0] + ' [OPTIONS]' + print ''' +Options: + -h, --help show this help message and exit + -c, --curses use curses (text mode) interface + -d, --daemon run in daemon (background) mode + +All parameters are optional. +''' + def stop(self): with shared.printLock: print('Stopping Bitmessage Deamon.')