bash/shell to python

Cameron Simpson cs at zip.com.au
Sun May 20 23:39:10 EDT 2012


On 19May2012 08:27, Michael Torrie <torriem at gmail.com> wrote:
| On 05/16/2012 08:16 PM, Rita wrote:
| > I currently build a lot of interfaces/wrappers to other applications
| > using bash/shell. One short coming for it is it lacks a good method
| > to handle arguments so I switched to python a while ago to use
| > 'argparse' module.
|| Actually there is a great way of parsing command line options in
| bash, using the GNU "getopt" program. See:
| http://www.manpagez.com/man/1/getopt/
|| This command is available on all Linux systems, and most BSD systems.
| There's also freegetopt, a BSD implementation for unix, MSDOS, or Windows.

And if you don't want to do option bundling it's easy enough to write a
simple option parser without getopt at all.
My standard option parsing looks somewhat like this:
 # some things to control via the options
 the_file=
 doit=1
 trace=
 # badness?
 badopts=
 while [ $# -gt 0 ]
 do
 case 1ドル in
 -f) the_file=2ドル; shift ;;
 -n) doit= ;;
 -x) trace=1 ;;
 --) shift; break ;;
 -?*) echo "0ドル: unrecognised option: 1ドル" >&2
 badopts=1
 ;;
 *) break ;;
 esac
 shift
 done
 # ... parse non-option arguments ...
 [ $badopts ] && { echo "$usage" >&2; exit 2; }
 # ... rest of program ...
There's nothing hard there.
Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/
Any profit should go to Arnie's `get the daemon carved on Mount Rushmore' fund.
 - Marty Albini, DOD0550, martya at sdd.hp.com


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /