command-line arguments processing in bash - openalt · 2017-11-29 · command-line interface what...
TRANSCRIPT
![Page 1: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/1.jpg)
1 / 24
Command-linearguments processingin bash
Matěj Týč, OpenAlt 2017
![Page 2: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/2.jpg)
2 / 24
Outline
● How people tend to design interface of their bash scripts?
● What exactly is command-line interface?● How is bash diferent from other languages?● What is the bash way?● What really is the bash way.
![Page 3: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/3.jpg)
3 / 24
Bash scripts
Postgres backup script from postgres wiki
![Page 4: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/4.jpg)
4 / 24
Bash scripts
The bash docker (bocker) – example of a good script (as Awesome Bash says).
![Page 5: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/5.jpg)
5 / 24
Bash scripts
Ways how to pass data to your script:
● Env variables: LS_LONG=true ls● “Confgg fles: ls ls_config_long.cfg● Command-line arguments: ls --long
![Page 6: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/6.jpg)
6 / 24
Command-line interface
Let’s write an ls help script ls-help. In bash!
● # default argument$ ls-help> List all files in directory .
● # Multiple arguments,# the default is replaced$ ls-help foo bar> List all files in directories ‘foo’, ‘bar’
![Page 7: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/7.jpg)
7 / 24
Command-line interface
Let’s write an ls help script ls-help. In bash!
● # Short boolean option$ ls-help -A foo bar> List all files in directories ‘foo’, ‘bar’> Do not list implicit ‘.’ and ‘..’
● # Long boolean option, reversed order# does the same$ ls-help foo bar --almost-all> List all files in directories ‘foo’, ‘bar’> Do not list implicit ‘.’ and ‘..’
![Page 8: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/8.jpg)
8 / 24
Command-line interface
Let’s write an ls help script ls-help. In bash!
● # Long option that takes value$ ls-help --width 10> List all files in directory ‘.’> Pad the width to 10 terminal columns
● # Separated by = is the same$ ls-help --width=10> List all files in directory ‘.’> Pad the width to 10 terminal columns
![Page 9: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/9.jpg)
9 / 24
Command-line interface
Let’s write an ls help script ls-help. In bash!
● # Short option and value can be stacked$ ls-help -w10> List all files in directory ‘.’> Pad the width to 10 terminal columns
● # Short options can be stacked$ ls-help -Aw10> List all files in directory ‘.’> Do not list implicit ‘.’ and ‘..’> Pad the width to 10 terminal columns
![Page 10: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/10.jpg)
10 / 24
Command-line interface
Let’s write an ls help script ls-help. In bash!
● # Optional and positional arguments# can be sparated by --$ ls-help -A -- -w10> List all files in directory ‘-w10’> Do not list implicit ‘.’ and ‘..’
● # Some arguments can be appended to a list$ ls-help -I ‘*.bak’ -I ‘*~’> List all files in directory ‘.’> Do not list item that match shell patterns ‘*.bak’ and ‘*~’
![Page 11: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/11.jpg)
11 / 24
Command-line interface
Let’s write an ls help script ls-help. In bash!
● # We want a nice help message$ ls-help -h> ls-help [-h] [-A|--almost-all] ...> -w|--width <int>: Padded width> ...
![Page 12: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/12.jpg)
12 / 24
Command-line interface
What defnes command-line interface:● Positional arguments: Strings that have
meaning on their own, is the position on command-line that identifes them
● Optional argumetns: They are either options or option-value pairs. Options are prefxed by single or double dash.
● $ ls -w 10 something● $ ls -A 10 something
![Page 13: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/13.jpg)
13 / 24
Command-line interface
What defnes command-line interface:
POSIX standard:
$ script [opt. args] [--] pos. args● Only short options were allowed.● Order matters – no optional args after frst
positional arg
![Page 14: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/14.jpg)
14 / 24
Command-line interface
What defnes command-line interface:
GNU standard:
$ script [opt. args] [--] pos. args● Same as POSIX, but more permissive.● Long options were allowed.● Order doesn’t matter – optional args may
follow positional and still be treated as optional.
![Page 15: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/15.jpg)
15 / 24
Non-bash
There are plenty of arg parsing libraries:
Language UtilityPython Python argparsePerl Getopt::LongC Getopt, popt, C argparseC++ Boost::ProgramOptions, QCommandLineParserLua Lua ArgparseGo Flag
![Page 16: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/16.jpg)
16 / 24
Bash experts
![Page 17: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/17.jpg)
17 / 24
Bash experts
![Page 18: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/18.jpg)
18 / 24
Bash experts
![Page 19: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/19.jpg)
19 / 24
Non-bash
There are plenty of arg parsing libraries:
Library Family Note
Argbash code generator ???
argparse-bash module Uses Python
EasyOptions module Lacks features
getopt executable Not portable
getopts builtin Short options only
shflags library Uses getopt
Bash-modules argument module Lacks features
...
![Page 20: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/20.jpg)
20 / 24
Why argbash
● Modules
– are pain to distribute for users.– create a huge external dependency.– usually have bad documentation.
● Builtins are horrible to use,● but still better than 100% manual parsing.
![Page 21: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/21.jpg)
21 / 24
Why argbash
● Code generators:
– Need to be installed.– Have to be able to regenerate modifed
script.– Should better generate nice code.
● You still have to ship the generated code.
![Page 22: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/22.jpg)
22 / 24
Why use argbash
● Generated code is a template.● Generates simple code for simple scripts,
complex code for complex ones.● Available as:
– Local install: Github, Fedora, AUR– Docker image: Search the docker hub– Online service https://argbash.io/generate
● Feature-rich and documented.
![Page 23: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/23.jpg)
23 / 24
DEMO
Clone/download sample fles from
https://github.com/matejak/argbash-demo
![Page 24: Command-line arguments processing in bash - OpenAlt · 2017-11-29 · Command-line interface What defnes command-line interface: Positional arguments: Strings that have meaning on](https://reader030.vdocuments.us/reader030/viewer/2022040116/5f0a40e87e708231d42ac181/html5/thumbnails/24.jpg)
24 / 24
Thank you attention!
Don’t forget to https://argbash.io
Any questions?