/
genopts_example.gopts
78 lines (52 loc) · 2.07 KB
/
genopts_example.gopts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#! /bin/bash
# Your genopts section can start anywhere in code.
@genopts
@code_style line_up function_keyword tabs casetab
@program genopts_example
@version 1.2
@year 2004
@author Stanislav Brabec
@bugsto Stanislav Brabec <utx@penguin.cz>
@free_copy
@usage [OPTION]... [FILE]...
@short
shows an example of all genopts features
@defaults MYOPTIONS
there can be defaults stored in variable
@switch SWI -s --switch
shows usage of simple switch
@cntswitch VER -v --verbose
shows usage of countable switch
@option VAL -d --value=VALUE
shows usage of option with value
@multioption MULVAL -m --multi-value=VALUE
shows usage of option with value, which can be used multiple times
@multioption MULPRE --multi-predef=VALUE default1 "multiple words in default 2"
multioptions can have defaults
@option VALD -x --dvalue=VALUE "this string"
shows usage of option with value and default
@switch LONGO --longonly
switches and options can be long only and short only
Switch description can also be multi-line.
@option SHO -S=VALUE
use this trick for defining short option with value
@end
# And there start our code:
echo "There is result of parsing:"
$OPTARG_SWI && echo "Simple switch was present."
$OPTARG_VER && echo "Countable verbose switch was present $OPTCNT_VER times."
$OPTARG_VAL && echo "Value option was present with value $OPTVAL_VAL."
$OPTARG_MULVAL && echo "Multi value option was present ${#OPTVAL_MULVAL[@]} times with values ${OPTVAL_MULVAL[*]}."
# Note that $OPTARG_MULPRE is false, if option is not present, but defaults are filles:
$OPTARG_MULPRE && echo "Multi value option with default has ${#OPTVAL_MULPRE[@]} values: ${OPTVAL_MULPRE[*]}."
if $OPTARG_VALD
then
echo "Dvalue option with default was present and value is $OPTVAL_VALD."
else
echo "Dvalue option with default was not present and value is $OPTVAL_VALD."
fi
$OPTARG_LONGO && echo "Long only switch was present."
$OPTARG_SHO && echo "Short value option was present with value $OPTVAL_SHO."
echo "And non-option arguments were: \"${ARGV[*]}\"" # We can use ${ARGV[@]},
# see bash(1) for differences.
exit 0