File: 8-setting-hook

package info (click to toggle)
bash-argsparse 1.8.22.ga05fe6d-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: sh: 1,798; sed: 100; makefile: 6
file content (78 lines) | stat: -rwxr-xr-x 2,070 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env bash

PATH="..:$PATH"

# Load argsparse library.
. /usr/share/bash-argsparse/argsparse.sh

# You can also change the way options are set by defining a function
# named set_option_<optionname>.
argsparse_use_option option1 "An option." value type:hexa

set_option_option1() {
	local option=$1
	local value=$2
	# You can do own stuff here. Whatever you want, including,
	# transforming the value, calling other functions, performing some
	# actions..
	# E.g: you could enforce the 0x in front of a user-given
	# hexadecimal value.
	if [[ "$value" != 0x* ]]
	then
		value="0x$value"
	fi
	# This is the original argsparse setting action.
	argsparse_set_option_with_value "$option" "$value"
}

# For options without value.
argsparse_use_option option2 "Another option."

set_option_option2() {
	local option=$1
	# Again, you can do whatever you want here.
	: some command with some params.
	# You dont have to do it, but it makes sense to call in the end
	# the original argsparse action.
	argsparse_set_option_without_value "$option"
}

# This is a way to re-implement a cumulative option if you're
# satisfied with the 'cumulative' property.
argsparse_use_option cumul "A cumulative option." value

all_cumul_values=()
set_option_cumul() {
	local option=$1
	local value=$2
	# Append the value to the array.
	all_cumul_values+=( "$value" )
	# Doing this will prevent the argsparse_is_option_set test from
	# returning false.
	argsparse_set_option_with_value "$option" 1
}

# 
printf -v argsparse_usage_description "%s\n" \
	"A tutorial script teaching how to change the way options are set." \
	"Try command lines such as:" \
	" $0" \
	" $0 -h" \
	" $0 --option1 123a" \
	" $0 --option2" \
	" $0 --cumul first --cumul second --cumul other"

# Command line parsing is done here.
argsparse_parse_options "$@"

printf "Options reporting:\n"
# Simple reporting function.
argsparse_report
printf "End of argsparse report.\n\n"

printf "These are all the 'cumul' option values:\n"
i=0
for v in ${all_cumul_values[@]}
do
	printf "Value #%d: %s\n" $((++i)) "$v"
done