Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mbedsys
bashopts
Commits
97dd4ff7
Commit
97dd4ff7
authored
Feb 05, 2017
by
Emeric Verschuur
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix spaces in string value
parent
ff682117
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
16 deletions
+18
-16
README.md
README.md
+10
-10
bashopts.sh
bashopts.sh
+6
-4
example.sh
example.sh
+2
-2
No files found.
README.md
View file @
97dd4ff7
...
...
@@ -67,7 +67,7 @@ bashopts_parse_args <arguments...>
Code example:
```
bash
bashopts_parse_args
$@
bashopts_parse_args
"
$@
"
```
### Argument processing
...
...
@@ -113,7 +113,7 @@ bashopts_declare -n age -l number -d "Age" -t number
bashopts_declare
-n
email_list
-t
string
-m
add
-l
email
-d
"Email adress"
# Parse arguments
bashopts_parse_args
$@
bashopts_parse_args
"
$@
"
# Process argument
bashopts_process_args
...
...
@@ -124,7 +124,7 @@ echo "First name: $first_name"
echo
"Last name:
$last_name
"
echo
"Display name:
$display_name
"
echo
"Age:
$age
"
for
email
in
${
email_list
[@]
}
;
do
for
email
in
"
${
email_list
[@]
}
"
;
do
echo
"Email:
$email
"
done
```
...
...
@@ -143,13 +143,13 @@ USAGE:
[options and commands] [-- [extra args]]
OPTIONS:
-h,--help Display this help
-n,--non-interactive Non interactive mode -
boolean
bashopts_non_interactive
=true (
default:false)
-f,--first First name -
string
first_name
="John" (
default:"")
-l,--last Last name -
string
last_name
="Smith" (
default:"")
--display-name
Display name -
string
display_name
="John Smith" (
default:"$first_name $last_name")
--number Age -
number age=0 (
default:0)
--email Email adress -
string
email_list
="" (
default:"")
-h,--help
Display this help
-n,--non-interactive
true
Non interactive mode -
[$
bashopts_non_interactive
] (type:boolean,
default:false)
-f,--first
"John"
First name -
[$
first_name
] (type:string,
default:"")
-l,--last
"Smith"
Last name -
[$
last_name
] (type:string,
default:"")
--display-name
"John Smith"
Display name -
[$
display_name
] (type:string,
default:"$first_name $last_name")
--number
0
Age -
[$age] (type:number,
default:0)
--email
[""]
Email adress -
[$
email_list
] (type:string,
default:"")
```
Test with arguments:
...
...
bashopts.sh
View file @
97dd4ff7
...
...
@@ -263,19 +263,21 @@ bashopts_dump_value() {
fi
return 0
fi
set --
"
${
!
op[@]
}
"
eval
set --
\
"
\$
{
${
op
}
[@]}
\
"
echo -n "
[
"
if [ "
${
bashopts_optprop_type
[
$op
]
}
" == "
string
" ]; then
echo -n "
\
"
${
1
//\
"/
\\\"
}
\"
"
else
echo -n
"
${
1
}
"
fi
while shift; do
shift
while [ -n
"
$1
"
]; do
if [
"
${
bashopts_optprop_type
[
$op
]
}
"
==
"string"
]; then
echo -n
",
\"
${
1
//\
"/
\\\"
}
\"
"
else
echo -n
",
${
1
}
"
fi
shift
done
echo -n
"]"
}
...
...
@@ -374,7 +376,7 @@ bashopts_parse_args() {
break
;;
-*)
val=
$1
val=
"
$1
"
shift
op=
${
bashopts_opt2name
[
$arg
]
}
case "
${
bashopts_optprop_type
[
$op
]
}
" in
...
...
@@ -400,7 +402,7 @@ bashopts_parse_args() {
esac
;;
*)
error "
Fatal
error
:
args
"
bashopts_
error "
Fatal
error
:
args
"
;;
esac
done
...
...
example.sh
View file @
97dd4ff7
...
...
@@ -17,7 +17,7 @@ bashopts_declare -n age -l number -d "Age" -t number
bashopts_declare
-n
email_list
-t
string
-m
add
-l
email
-d
"Email adress"
# Parse arguments
bashopts_parse_args
$@
bashopts_parse_args
"
$@
"
# Process argument
bashopts_process_args
...
...
@@ -28,6 +28,6 @@ echo "First name: $first_name"
echo
"Last name:
$last_name
"
echo
"Display name:
$display_name
"
echo
"Age:
$age
"
for
email
in
${
email_list
[@]
}
;
do
for
email
in
"
${
email_list
[@]
}
"
;
do
echo
"Email:
$email
"
done
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment