Skip to content

Commit

Permalink
Set exit status to 0 for --help. Fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaberez committed Sep 5, 2016
1 parent d5706d7 commit 266636a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- ``httpok`` now has a ``--name`` option like ``memmon``.

- All commands now return exit status 0 from ``--help``.

0.12 (2016-09-03)
-----------------

Expand Down
8 changes: 4 additions & 4 deletions superlance/crashmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@
"""

import getopt
import os
import sys

from supervisor import childutils


def usage():
def usage(exitstatus=255):
print(doc)
sys.exit(255)
sys.exit(exitstatus)


class CrashMail:
Expand Down Expand Up @@ -145,7 +146,6 @@ def mail(self, email, subject, msg):


def main(argv=sys.argv):
import getopt
short_args = "hp:ao:s:m:"
long_args = [
"help",
Expand All @@ -170,7 +170,7 @@ def main(argv=sys.argv):
for option, value in opts:

if option in ('-h', '--help'):
usage()
usage(exitstatus=0)

if option in ('-p', '--program'):
programs.append(value)
Expand Down
14 changes: 8 additions & 6 deletions superlance/httpok.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"""

import getopt
import os
import socket
import sys
Expand All @@ -113,9 +114,9 @@

from superlance import timeoutconn

def usage():
def usage(exitstatus=255):
print(doc)
sys.exit(255)
sys.exit(exitstatus)

class HTTPOk:
connclass = None
Expand Down Expand Up @@ -315,7 +316,6 @@ def restart(self, spec, write):


def main(argv=sys.argv):
import getopt
short_args="hp:at:c:b:s:m:g:d:eE"
long_args=[
"help",
Expand All @@ -337,6 +337,11 @@ def main(argv=sys.argv):
except:
usage()

# check for -h must be done before positional args check
for option, value in opts:
if option in ('-h', '--help'):
usage(exitstatus=0)

if not args:
usage()
if len(args) > 1:
Expand All @@ -357,9 +362,6 @@ def main(argv=sys.argv):

for option, value in opts:

if option in ('-h', '--help'):
usage()

if option in ('-p', '--program'):
programs.append(value)

Expand Down
15 changes: 9 additions & 6 deletions superlance/memmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
memmon.py -p program1=200MB -p theprog:thegroup=100MB -g thegroup=100MB -a 1GB -s "/usr/sbin/sendmail -t -i" -m [email protected] -n "Project 1"
"""

import getopt
import os
import sys
import time
Expand All @@ -90,9 +91,9 @@
from supervisor import childutils
from supervisor.datatypes import byte_size, SuffixMultiplier

def usage():
def usage(exitstatus=255):
print(doc)
sys.exit(255)
sys.exit(exitstatus)

def shell(cmd):
with os.popen(cmd) as f:
Expand Down Expand Up @@ -333,8 +334,9 @@ def parse_seconds(option, value):
usage()
return seconds

help_request = object() # returned from memmon_from_args to indicate --help

def memmon_from_args(arguments):
import getopt
short_args = "hcp:g:a:s:m:n:u:"
long_args = [
"help",
Expand Down Expand Up @@ -367,7 +369,7 @@ def memmon_from_args(arguments):
for option, value in opts:

if option in ('-h', '--help'):
return None
return help_request

if option in ('-c', '--cumulative'):
cumulative = True
Expand Down Expand Up @@ -408,8 +410,9 @@ def memmon_from_args(arguments):

def main():
memmon = memmon_from_args(sys.argv[1:])
if memmon is None:
# something went wrong or -h has been given
if memmon is help_request: # --help
usage(exitstatus=0)
elif memmon is None: # something went wrong
usage()
memmon.rpc = childutils.getRPCInterface(os.environ)
memmon.runforever()
Expand Down
9 changes: 4 additions & 5 deletions superlance/process_state_email_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# FITNESS FOR A PARTICULAR PURPOSE
#
##############################################################################
import copy
import optparse
import os
import sys
import smtplib
import copy
import sys

from email.mime.text import MIMEText
from email.utils import formatdate, make_msgid
Expand All @@ -31,9 +32,7 @@ class ProcessStateEmailMonitor(ProcessStateMonitor):

@classmethod
def _get_opt_parser(cls):
from optparse import OptionParser

parser = OptionParser()
parser = optparse.OptionParser()
parser.add_option("-i", "--interval", dest="interval", type="float", default=1.0,
help="batch interval in minutes (defaults to 1 minute)")
parser.add_option("-t", "--toEmail", dest="to_emails",
Expand Down
12 changes: 8 additions & 4 deletions superlance/tests/memmon_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import unittest
from superlance.compat import StringIO
from superlance.compat import maxint
from superlance.memmon import memmon_from_args
from superlance.memmon import seconds_size
from superlance.memmon import (
help_request,
memmon_from_args,
seconds_size
)
from superlance.tests.dummy import DummyRPCServer

class MemmonTests(unittest.TestCase):
Expand Down Expand Up @@ -348,8 +351,9 @@ def test_argparser(self):
# help
arguments = ['-h', ]
memmon = memmon_from_args(arguments)
self.assertTrue(memmon is None, '-h returns None to make main() script print usage')

self.assertTrue(memmon is help_request,
'-h returns help_request to make main() script print usage'
)

#all arguments
arguments = ['-c',
Expand Down

0 comments on commit 266636a

Please sign in to comment.