Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion argparse.bash
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class MyArgumentParser(argparse.ArgumentParser):
sys.exit(1)

parser = MyArgumentParser(prog=os.path.basename("$0"),
description="""$ARGPARSE_DESCRIPTION""")
description="""$ARGPARSE_DESCRIPTION""",
epilog="""$ARGPARSE_EPILOG""")
EOF

# stdin to this function should contain the parser definition
Expand Down
7 changes: 5 additions & 2 deletions example.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env bash

ARGPARSE_DESCRIPTION="Sample script description" # this is optional
source $(dirname $0)/argparse.bash || exit 1
ARGPARSE_EPILOG="This is the epilog." # this is optional
# shellcheck source=/Users/aberezin/apps/argparse.bash
source "$ARGPARSE_PATH" || exit 1
#optional way to plave src file: source $(dirname $0)/argparse.bash || exit 1
argparse "$@" <<EOF || exit 1
parser.add_argument('infile')
parser.add_argument('outfile')
Expand All @@ -16,7 +19,7 @@ EOF
echo required infile: "$INFILE"
echo required outfile: "$OUTFILE"
echo the answer: "$THE_ANSWER"
echo -n do the thing?
echo -n "do the thing?"
if [[ $DO_THE_THING ]]; then
echo " yes, do it"
else
Expand Down
15 changes: 12 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
from textwrap import dedent


def run(script, *args):
def run(script,*args,expect_exit_1=False):
cmd = [script] + list(args)
if hasattr(subprocess, 'check_output'):
# python 2.7+
output = subprocess.check_output(cmd, universal_newlines=True).strip()
try:
output = subprocess.check_output(cmd, universal_newlines=True).strip()
except subprocess.CalledProcessError as cpe:
if not expect_exit_1:
raise cpe
if cpe.returncode != 1:
raise cpe
output = cpe.output
else:
# python 2.6
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].strip()

return output


Expand Down Expand Up @@ -57,6 +63,9 @@ def test05(self):
output = run('./argparse.bash')
self.assertTrue(output.startswith('#!/usr/bin/env bash'))

def test06(self):
output = run('./example.sh','-h', expect_exit_1 = True )
self.assertTrue('epilog' in output)

if __name__ == '__main__':
unittest.main()