0

I know that a single dash goes with one character when we set options using getopt or optparse. but is there anyway to use a single dash with more than one character, like -apply?

I know that a single dash refers to a short name, and conventionally people use one character for it. but there are some commands (eg, find -printf) that use long name with a single dash.

Does anyone know how to use long name with a single dash in bash on Linux (using getopt or optparse other bash tools)?

terdon
  • 242,166
Lena
  • 9
  • 1
  • 2

3 Answers3

2

From getopt's manpage:

-a, --alternative
        Allow long options to start with a single '-'.

Or, if we're talking about the getopt* C functions:

getopt_long_only() is like getopt_long(), but '-' as well as "--" can indicate a long option
mreithub
  • 3,583
  • getopt_long is a GNU extension... – ilkkachu Aug 24 '16 at 13:32
  • @ilkkachu yes, but the OP had tagged this with Linux, so that's what they'll be using. – terdon Aug 24 '16 at 13:46
  • @terdon if there are some other linux commands you would like to recommend to me, please let me know. I don't really have to use OP. – Lena Aug 24 '16 at 13:55
  • @Lena OP here means "original post" (your question) or "original poster" (you). I'm not sure what you mean here. In any case, the tools you use will depend on what you're doing. There are getopts or equivalent functions in pretty much all programming languages and they don't al behave the same way. – terdon Aug 24 '16 at 14:05
0

If you have a GNU system, and want single-dash long options, then by all means use getopt_long or getopt -a.

The example of find -printf was what lead me to note about the GNU extensions: The one-dash options for find exist outside GNU systems too, and the heritage is likely much older than that of the GNU-style long options. So programs like find probably don't use getopt_long, but just parse their command line options by hand. In bash, it might look something like this:

$ cat args.sh
#!/bin/sh
foo=0
bar=0
while [ "$#" -ne 0 ] ; do 
        if [ "$1" = "-foo" ] ; then
                foo=1
        elif [ "$1" = "-bar" ] ; then
                shift
                bar=$1
        fi
        shift
done
echo foo: "$foo"
echo bar: "$bar"
$ ./args.sh -bar x -foo
foo: 1
bar: x
ilkkachu
  • 138,973
0

The difficulty with parsing single-character option is that they can be bundled (e.g. -ab is equivalent to -a -b). GNU-style long options have the difficulty that arguments can be joined or not. With single-dash multi-character options, there isn't any parsing difficulty so you can easily do it by hand.

Most programs with single-dash multi-character options take arguments in a separate command line argument.

foo=
bar=default
require_argument () {
  if [ $# -eq 2 ]; then
    echo >&2 "$0: Option $1 requires an argument"
    exit 2
  fi
  eval "$1=\"\$3\""
}
while case "$1" in
        --) shift; false;;
        -foo) foo=1 
        -bar) require_argument bar "$@"; shift;;
        -?*) echo >&2 "$0: Unknown option $1"; exit 2;;
        *) false;;
      esac
do
  shift
done

Do consider using GNU-style long options. They have more standardized behavior and more people are familiar with them.