linux - getopts: How to accept arguments that aren't tied to an option in my script? -
this question has answer here:
- how parse command line arguments in bash? 28 answers
i'm writing bash script has following usage:
ci-badge -t|-a [-b branch] [-m] [-d description] [-l [link] ] repo
examples:
$ ci-badge -t foo/bar $ ci-badge -ab dev foo/bar -m $ ci-badge quux/bar -md 'hello, world.'
more samples can found here on github. anyway, i'm wondering how implement argument parsing script using getopts
. after few hours looking @ this basic getopts
guide , scouring so, code looks far:
#!/usr/bin/env bash generate_url=false # set true if -l passed no arg markdown=false # true -> surround url markdown # parse options while getopts ':tab:md:l:' opt case "$opt" in a) service=appveyor ;; b) branch=$optarg ;; d) description=$optarg ;; l) url=$optarg ;; m) markdown=true ;; t) service=travis ;; # colon: runs if no args passed # option requires parameters. :) test "$optarg" = l && generate_url=true ;; # ?: runs if invalid option passed. '?') die ;; esac done
as can see, of functionality there, i'm wondering how accept repo
argument script. since getopts
stops parsing after encounters first non-option argument, i'm wondering, how implement (preferably minimal complexity)? guide linked earlier doesn't seem mention dealing arguments aren't associated option, i'm bit lost.
thanks helping!
use $optind value. after getopts cycle:
shift $((optind-1)) echo $@ echo $1 $2 ...
Comments
Post a Comment