Awesome Video again! The subparsers were new to me. Now I know how they work ;-) I personally like using formatter_class=argparse.ArgumentDefaultsHelpFormatter in the initial parser definition so the default is always displayed in the help when there is one. Keep up the great work!
I was attempting to put together my own way to parse arguments, until I came across argparse. Then I happened upon your video, which was very informative and helpful! Big thumbs up for this!
David: *talks about how he's working on a python CLI during stream* Anthony (who writes code, btw): "Have you seen my argparse wideo?" RUclips: "Hey David, checkout this cool video from Mr. Writes code about argparse David: "THE AI IS TOO GOOD, HOW DID IT CROSS INTO TWITCH?"
Great video Anthony, I'll definitely use it as my go-to recommendation for people new to argparse. :) Regarding the `action='append'` use case: I was writing a port scanner the other day and knowing of it would've helped me a lot. :D This way you can specify multiple ports or multiple target machines.
Perhaps added to argparse after this video was published but I've recently discovered `action=argparse.BooleanOptionalAction` and it's wonderful. For example if you add an argument "--save-output" you can call --save-output and it will be set to True or you can call "--no-save-output" and it will be set to False. Very useful feature
I'm actually not using an IDE at all! this is my text editor that I wrote -- you can learn more about it here: ruclips.net/video/WyR1hAGmR3g/видео.html -- entirely in the terminal!
Hello AWC, this is certainly my goto tutorial for refreshing my knowledge on argparse. I'm working on a piece of code which would need an optional-positional file. But I don't want to use short/long options for it. Any idea how to do that? I want something like python's argparsing thingy... if I do $ python3 ... this invokes the python interpreter but if I do $ python3 t.py it executes the script. How to implement this optional-positional (script) argument?
@@anthonywritescode Well that was my first approach but unfortunately it didn't work. This method works fine if I don't pass any filename as arg. But if I pass the file name like $ langxa ./changelog.rst - I get this error: `langxa: error: argument command: invalid choice: './changelog.rst' (choose from 'log')`
@@anthonywritescode danggit! Yes I’ve subcommand. This was inspired from your code from precommit.main.main() function. I tried to use your approach of having a sub parser for sub commands.
Thanks, Anthony. What are your goto standard libraries while playing with text files or text parsing or generally working with text? (Asking this as an infra eng handling servers) maybe a playlist of videos would definitely be helpful if you kindly do plan your future topics :)
I typically just use the standard library -- I'm not really a fan of pandas/numpy/etc. -- they're severely overkill for most tasks. all of these videos are in a playlist! the easiest way to ingest it isn't through youtube's playlist features though so I put them on github github.com/anthonywritescode/explains
yes, you can do `nargs='+'` for example -- though it's not usually a good idea since it can break the rest of the argument parsing -- usually `action='append'` is better
Code does not work in beginning: TypeError: main() missing 1 required positional argument: 'argv' This happens even after I type python parse.py hello world
you must have forgotten the `= None` part ? github.com/anthonywritescode/explains/blob/32d27aa9480bf67492d31d614bf5fae3746bfff2/sample_code/ep044/rev01/t.py#L7
That was the best explanation i ever seen about argparse, thank you sir. Sir what about how to check them ? I mean check if "status" and "--force" defined and do some code upon that.
it's explained in this video! but also I've switched to instead `raise SystemExit(main())` -- more on that here: ruclips.net/video/ZbeSPc5wL0g/видео.html
The best video on argparse that I've ever seen. Really great video. Thanks for sharing.
Awesome Video again! The subparsers were new to me. Now I know how they work ;-)
I personally like using formatter_class=argparse.ArgumentDefaultsHelpFormatter in the initial parser definition so the default is always displayed in the help when there is one.
Keep up the great work!
I was attempting to put together my own way to parse arguments, until I came across argparse. Then I happened upon your video, which was very informative and helpful! Big thumbs up for this!
glad you enjoyed it -- let me know if there's other stuff you'd like me to talk about as well :)
David: *talks about how he's working on a python CLI during stream*
Anthony (who writes code, btw): "Have you seen my argparse wideo?"
RUclips: "Hey David, checkout this cool video from Mr. Writes code about argparse
David: "THE AI IS TOO GOOD, HOW DID IT CROSS INTO TWITCH?"
IT KNOWS
Great video Anthony, I'll definitely use it as my go-to recommendation for people new to argparse. :)
Regarding the `action='append'` use case: I was writing a port scanner the other day and knowing of it would've helped me a lot. :D This way you can specify multiple ports or multiple target machines.
yeah! append is super useful for repeated arguments
Excellent tutorial
Thank you, Anthony.
Perhaps added to argparse after this video was published but I've recently discovered `action=argparse.BooleanOptionalAction` and it's wonderful. For example if you add an argument "--save-output" you can call --save-output and it will be set to True or you can call "--no-save-output" and it will be set to False. Very useful feature
What IDE do you use to write your code and you have your command line open on a separate window and not part of IDE, correct?
I'm actually not using an IDE at all! this is my text editor that I wrote -- you can learn more about it here: ruclips.net/video/WyR1hAGmR3g/видео.html -- entirely in the terminal!
Hello AWC, this is certainly my goto tutorial for refreshing my knowledge on argparse. I'm working on a piece of code which would need an optional-positional file. But I don't want to use short/long options for it. Any idea how to do that? I want something like python's argparsing thingy... if I do $ python3 ... this invokes the python interpreter but if I do $ python3 t.py it executes the script. How to implement this optional-positional (script) argument?
should just be `parser.add_argument('positional', nargs='?')`
@@anthonywritescode Well that was my first approach but unfortunately it didn't work. This method works fine if I don't pass any filename as arg. But if I pass the file name like $ langxa ./changelog.rst - I get this error: `langxa: error: argument command: invalid choice: './changelog.rst' (choose from 'log')`
seems like you also have `choices=` or a subcommand? can't mix positional arguments with subcommands unfortunately
@@anthonywritescode danggit! Yes I’ve subcommand. This was inspired from your code from precommit.main.main() function. I tried to use your approach of having a sub parser for sub commands.
Hi Anthony! Is there a way to create a CLI that accepts arbitrary arguments the same way that a python function can accept kwargs?
yep! with either nargs or parse_known_args depending on the specific use case
@anthonywritescode thank you!!
The video is very helpful. Thank a ton!
Can you advise on the following: I'm trying to get the string 'price
yep! you can use a custom `type=` callback and raise argparse.ArgumentTypeError if it's wrong
wow, just wow. Thank you and subbed :)
Thanks, Anthony. What are your goto standard libraries while playing with text files or text parsing or generally working with text? (Asking this as an infra eng handling servers) maybe a playlist of videos would definitely be helpful if you kindly do plan your future topics :)
I typically just use the standard library -- I'm not really a fan of pandas/numpy/etc. -- they're severely overkill for most tasks.
all of these videos are in a playlist! the easiest way to ingest it isn't through youtube's playlist features though so I put them on github github.com/anthonywritescode/explains
@@anthonywritescode thank you legend!
Is there a way to store multiple names under 1 argument name? For example instead of typing --log multiple times type --log 1.log 2.log etc.
yes, you can do `nargs='+'` for example -- though it's not usually a good idea since it can break the rest of the argument parsing -- usually `action='append'` is better
@@anthonywritescode Okay, thanks for quick response
Thanks ! very helpful
What is the purpose of subparsers? Are they simply grouping arguments?
they are subcommands. think like "git status" vs "git commit"
Code does not work in beginning: TypeError: main() missing 1 required positional argument: 'argv' This happens even after I type python parse.py hello world
you must have forgotten the `= None` part ? github.com/anthonywritescode/explains/blob/32d27aa9480bf67492d31d614bf5fae3746bfff2/sample_code/ep044/rev01/t.py#L7
This made argparse click and make sense
Awsome channel! ty.
That was the best explanation i ever seen about argparse, thank you sir.
Sir what about how to check them ? I mean check if "status" and "--force" defined and do some code upon that.
they'll all end up in the args object returned from parse_args
any thoughts about 'click' pkg?
yeah, it's nice, but imo over-complicated, slightly-too-magical, needs to be installed, and argparse works well enough
@@anthonywritescode what about "adding autocomplete" video?
I haven't found a good solution for python that isn't hella slow beyond hand writing the bash / zsh outputs once
great video!
why exit(main()) instead of main()?
it's explained in this video! but also I've switched to instead `raise SystemExit(main())` -- more on that here: ruclips.net/video/ZbeSPc5wL0g/видео.html
@@anthonywritescode Sorry, might have skipped it. Thanks, added to watch list. 🙏
It's not in standard library but I highly recommend typer over argparse.