Skip to content

Commit

Permalink
Print function name when using argparse() with shell functions
Browse files Browse the repository at this point in the history
When using argparse() to parse arguments of a shell function, it printed
"bash", instead of function name, in usage message. The change fixes it.
  • Loading branch information
rayx committed Jan 19, 2019
1 parent 97b7a0f commit 8ced5d2
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions argparse.bash
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
# MIT License - Copyright (c) 2015 Noah Hoffman

argparse(){
argparser=$(mktemp 2>/dev/null || mktemp -t argparser)
local cmd=${FUNCNAME[1]}
if [[ $cmd == "main" ]]; then
cmd=$0
fi

local argparser=$(mktemp 2>/dev/null || mktemp -t argparser)
cat > "$argparser" <<EOF
from __future__ import print_function
import sys
Expand All @@ -27,7 +32,7 @@ class MyArgumentParser(argparse.ArgumentParser):
super(MyArgumentParser, self).print_help(file=file)
sys.exit(1)
parser = MyArgumentParser(prog=os.path.basename("$0"),
parser = MyArgumentParser(prog=os.path.basename("$cmd"),
description="""$ARGPARSE_DESCRIPTION""")
EOF

Expand Down

0 comments on commit 8ced5d2

Please sign in to comment.