• Stars
    star
    167
  • Rank 226,635 (Top 5 %)
  • Language
    Shell
  • License
    MIT License
  • Created almost 10 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use python's argparse module in shell scripts

argparse.bash

Use Python's argparse module in shell scripts

The function argparse parses its arguments using argparse.ArgumentParser. The command line options are defined in the function's stdin. argparse.bash should be in the same directory as a script that uses it.

Python 2.7 or 3.5+ is required. See https://docs.python.org/2.7/library/argparse.html for a description of the python module. Note that some of the Python module's features won't work as expected (or at all) in this simplistic implementation.

Installation

Get argparse.bash:

wget https://raw.githubusercontent.com/nhoffman/argparse-bash/master/argparse.bash
chmod +x argparse.bash

Then move the file into the same directory as any scripts that will use it.

Alternatively, you can paste the body of the argparse() function into your script (in which case you would of course omit the line sourcing argparse.bash in the examples below).

Usage

Here's an example, example.sh:

#!/usr/bin/env bash

ARGPARSE_DESCRIPTION="Sample script description"      # this is optional
source $(dirname $0)/argparse.bash || exit 1
argparse "$@" <<EOF || exit 1
parser.add_argument('infile')
parser.add_argument('outfile')
parser.add_argument('-a', '--the-answer', default=42, type=int,
                    help='Pick a number [default %(default)s]')
parser.add_argument('-d', '--do-the-thing', action='store_true',
                    default=False, help='store a boolean [default %(default)s]')
parser.add_argument('-m', '--multiple', nargs='+',
                    help='multiple values allowed')
EOF

echo required infile: "$INFILE"
echo required outfile: "$OUTFILE"
echo the answer: "$THE_ANSWER"
echo -n do the thing?
if [[ $DO_THE_THING ]]; then
    echo " yes, do it"
else
    echo " no, do not do it"
fi
echo -n "arg with multiple values: "
for a in "${MULTIPLE[@]}"; do
    echo -n "[$a] "
done
echo

Example output of this script:

$ ./example.sh infile.txt "name with spaces.txt"
required infile: infile.txt
required outfile: name with spaces.txt
the answer: 42
do the thing? no, do not do it
arg with multiple values: []

Note that hyphens in the long option names are changed to underscores, and variables are all-caps (to be more bash-y).

Help text looks like this:

$ ./example.sh -h
usage: example.sh [-h] [-a THE_ANSWER] [-d] [-m MULTIPLE [MULTIPLE ...]]
                  infile outfile

Sample script description

positional arguments:
  infile
  outfile

optional arguments:
  -h, --help            show this help message and exit
  -a THE_ANSWER, --the-answer THE_ANSWER
                        Pick a number [default 42]
  -d, --do-the-thing    store a boolean [default False]
  -m MULTIPLE [MULTIPLE ...], --multiple MULTIPLE [MULTIPLE ...]
                        multiple values allowed

Error messages:

$ ./example.sh foo
usage: example.sh [-h] [-a THE_ANSWER] [-d] [-m MULTIPLE [MULTIPLE ...]]
                  infile outfile
example.sh: error: too few arguments
$ ./example.sh foo bar -n baz
usage: example.sh [-h] [-a THE_ANSWER] [-d] [-m MULTIPLE [MULTIPLE ...]]
                  infile outfile
example.sh: error: unrecognized arguments: -n baz

Executing argparse.bash (as opposed to sourcing it) prints a script template to stdout:

$ ./argparse.bash
#!/usr/bin/env bash

source $(dirname $0)/argparse.bash || exit 1
argparse "$@" <<EOF || exit 1
parser.add_argument('infile')
parser.add_argument('-o', '--outfile')

EOF

echo "INFILE: ${INFILE}"
echo "OUTFILE: ${OUTFILE}"

A few notes:

  • action=store_true or store_false provides a value of "yes" for True, "" for False
  • args='+' or args='*' provides an array of values.

License

MIT License (see LICENSE.txt)

Copyright (c) 2017 - 2022 Noah Hoffman

More Repositories

1

org-export

batch export of org-mode files from the command line
Emacs Lisp
64
star
2

bioscons

Extends the scons build tool for reproducible workflows in bioinformatics.
Python
17
star
3

.emacs.d

My emacs configuration
Emacs Lisp
16
star
4

alnvu

Reformat and condense multiple sequence alignments to highlight variability
Python
7
star
5

Seq

(abandoned) python utilities for manipulation of biological sequences
Python
7
star
6

swarmwrapper

Wraps swarm to perform pooled dereplication
Python
3
star
7

mkvenv

A wrapper for virtualenv, pip, and wheel
Python
3
star
8

rvenv

Install and manage R packages in a python virtualenv.
R
2
star
9

emacs-config

My emacs configuration, take 2
Emacs Lisp
2
star
10

barcodecop

Enforce barcode match stringency and read quality in demultiplexed MiSeq reads
Python
2
star
11

fastalite

Simplest possible fasta/fastq parser
Python
2
star
12

opiates

Automated QA for a clinical LC/MS urine opaites assay
Python
2
star
13

borborygmi

A blog built using emacs org-mode and Pelican
HTML
2
star
14

ya16sdb

A curated subset of 16S rRNA sequences from NCBI
Python
2
star
15

pymenu

Provides a simple, interactive text-based user interface
Python
2
star
16

unittools

Lightweight utility for running python unit tests
Python
2
star
17

uwgroups

Python library and CLI for UW Groups API
Python
1
star
18

16s-capture

Pipleine code for "Sensitive identification of bacterial DNA in clinical specimens by broad range 16S rRNA enrichment"
Python
1
star
19

mypy

Configuration for my python environment
Shell
1
star
20

dada2-nf

A Nextflow pipeline for processing 16S rRNA sequences using dada2
Python
1
star
21

ungapatchka

A python project template with bells and whistles
Python
1
star
22

deduplicate

Remove duplicates from collections of biological sequences
1
star
23

lab-software-whitepaper

A whitepaper describing software development best practices for the clinical laboratory
TeX
1
star