Тёмный

Parse Command Line Positional Arguments and Flags with Bash 

Nick Janetakis
Подписаться 20 тыс.
Просмотров 1,4 тыс.
50% 1

We'll cover short flags, long flags, flags with and without values and required positional args.
Hit the subscribe button to receive more videos like this!
REFERENCE LINKS
---------------------------------------------------
► nickjanetakis.com/blog/parse-...
COURSES
---------------------------------------------------
Courses I've created that focus on web dev and deployment topics.
► nickjanetakis.com/courses/
THE TOOLS I USE / GEAR
---------------------------------------------------
► nickjanetakis.com/blog/the-to...
FOLLOW ME ELSEWHERE
---------------------------------------------------
► Twitter: / nickjanetakis
► GitHub: github.com/nickjj
TIMESTAMPS
---------------------------------------------------
0:00 -- Intro
0:56 -- Displaying a usage message
2:44 -- Seeing how $@ works
3:19 -- Setting up variables for our inputs
4:26 -- A while loop and reducing the args until it's 0
6:23 -- Handling the help menu
7:41 -- Handling the interests (required flag)
9:11 -- Understanding how shift works
10:53 -- Handling being cool or not (optional flag)
12:05 -- Handling positional arguments
13:32 -- Exiting out if there's too many arguments
14:08 -- Validating our inputs
15:45 -- Performing your script's main purpose
16:12 -- Why I explicitly return 0 in the main function
16:52 -- For basic parsing you can skip the loop and read $1, $2, etc.

Опубликовано:

 

1 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 10   
@er63438
@er63438 29 дней назад
Great video, super helpful.
@carryonmaking
@carryonmaking Год назад
Incredible guide. Thank you.
@armynyus9123
@armynyus9123 Год назад
Good vid. Came also to the conclusion that parsing args like this is the way to go, compared to using getopt[s]. In a main function. Think only downside is that `foo=bar` can't be handled, would need a preprocessor. When the script is still in flux but already in use, I tend to generate help dynamically, from `cat $0` & grepping the switches between the case / esac in main, plus inline comments.
@NickJanetakis
@NickJanetakis Год назад
Thanks. Yep there's room for improvement to handle auto-generating the help menu. You could get cute with parsing comments as 1 line descriptions too. Sometimes I don't mind hard coding the help though. There's this idea of DRY for coding patterns but DAMP is not bad for certain things. Basically a little bit of duplication for the sake of optimizing for clarity (this often applies to writing test cases).
@armynyus9123
@armynyus9123 Год назад
@@NickJanetakis Rite. Plus: FuncSpecs BEFORE coding make ton of sense - I guess writing a consitent help description can really help straighten designs...
@Zancb
@Zancb Год назад
FYI, you can use `echo -e` to print out newlines. So it could have been: `echo -e "${1} must have a value " ` You're barely saving any characters using echo. Just thought you would want to know there's another way to do it.
@NickJanetakis
@NickJanetakis Год назад
Thanks!
@georgetait386
@georgetait386 7 месяцев назад
is there a way to doing the same functionality using the getopts built in syntax? It worked nicely with flags for me but struggled with flags and positional arguments, so this video was a great help! But I can't help but thinking that getopts syntax would be able to handle this in a much more elegant manner
@NickJanetakis
@NickJanetakis 7 месяцев назад
I haven't used getops a lot but based on stackoverflow.com/questions/11742996/is-mixing-getopts-with-positional-parameters-possible maybe not. It seems the limitation is when positional arguments appear before flags (check the comments in the marked answer). I'll admit I haven't looked into this too deeply tho. I Googled and found davetang.org/muse/2023/01/31/bash-script-that-accepts-short-long-and-positional-arguments/ but didn't really analyze to see if it would work with any order of flags and positional args.
@soufiane_elbk
@soufiane_elbk 20 дней назад
Ta7iya li Mohammed El Badry
Далее
Backstage or result?😈🔥 @milanaroller
00:12
Просмотров 7 млн
Creating a Bash script using flags
7:57
Просмотров 7 тыс.
Python programmer vs C programmer (speedrun)
1:43
Просмотров 429 тыс.
Haskell in 100 Seconds
2:30
Просмотров 825 тыс.