File: 5-custom-types

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 (35 lines) | stat: -rwxr-xr-x 954 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
#!/usr/bin/env bash

PATH="..:$PATH"

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

# Argsparse also allows user-defined types. To do so, just define a
# check_option_type_<your_type_name> function.
# E.g, for a `binary' type.

check_option_type_binary() {
	local value=$1
	# the function must return 0 if the provided argument (the value
	# submitted by the user) is valid.
	[[ "$value" =~ ^[01]+$ ]]
}

# This function allows you to give the 'type' property the value
# 'binary'.
argsparse_use_option bin-option "An binary word." type:binary value

printf -v argsparse_usage_description "%s\n" \
	"A example script using user-defined types." \
	"Try command lines such as:" \
	" $0 --bin-option something-wrong" \
	" $0 --bin-option 0101010101111"

# 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"