python argparse tutorial (beginner - intermediate) anthony explains

Поделиться
HTML-код
  • Опубликовано: 29 окт 2024

Комментарии • 48

  • @adwaithrajesh596
    @adwaithrajesh596 4 года назад +13

    The best video on argparse that I've ever seen. Really great video. Thanks for sharing.

  • @theendlessriver13
    @theendlessriver13 4 года назад +6

    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!

  • @itsdinguskhan4403
    @itsdinguskhan4403 4 года назад +2

    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!

    • @anthonywritescode
      @anthonywritescode  4 года назад

      glad you enjoyed it -- let me know if there's other stuff you'd like me to talk about as well :)

  • @DavidDellsperger
    @DavidDellsperger 2 года назад +1

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

  • @nilsmuller9286
    @nilsmuller9286 3 года назад +1

    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.

  • @DanielGalasso
    @DanielGalasso Год назад

    Excellent tutorial

  • @n.a.s1096
    @n.a.s1096 Год назад

    Thank you, Anthony.

  • @tommacnamara5430
    @tommacnamara5430 2 года назад

    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

  • @DennisZIyanChen
    @DennisZIyanChen 4 года назад +1

    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?

    • @anthonywritescode
      @anthonywritescode  4 года назад +3

      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!

  • @akshaymestry7503
    @akshaymestry7503 2 года назад +1

    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
      @anthonywritescode  2 года назад +1

      should just be `parser.add_argument('positional', nargs='?')`

    • @akshaymestry7503
      @akshaymestry7503 2 года назад

      @@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
      @anthonywritescode  2 года назад +1

      seems like you also have `choices=` or a subcommand? can't mix positional arguments with subcommands unfortunately

    • @akshaymestry7503
      @akshaymestry7503 2 года назад

      @@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.

  • @jake115577
    @jake115577 2 месяца назад

    Hi Anthony! Is there a way to create a CLI that accepts arbitrary arguments the same way that a python function can accept kwargs?

    • @anthonywritescode
      @anthonywritescode  2 месяца назад +1

      yep! with either nargs or parse_known_args depending on the specific use case

    • @jake115577
      @jake115577 2 месяца назад

      @anthonywritescode thank you!!

  • @tsankailieva
    @tsankailieva 2 года назад

    The video is very helpful. Thank a ton!
    Can you advise on the following: I'm trying to get the string 'price

    • @anthonywritescode
      @anthonywritescode  2 года назад

      yep! you can use a custom `type=` callback and raise argparse.ArgumentTypeError if it's wrong

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars 2 года назад +2

    wow, just wow. Thank you and subbed :)

  • @muddasirkhan805
    @muddasirkhan805 3 года назад +1

    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 :)

    • @anthonywritescode
      @anthonywritescode  3 года назад +1

      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

    • @muddasirkhan805
      @muddasirkhan805 3 года назад

      @@anthonywritescode thank you legend!

  • @dd-rv3xs
    @dd-rv3xs 3 года назад +1

    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.

    • @anthonywritescode
      @anthonywritescode  3 года назад

      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

    • @dd-rv3xs
      @dd-rv3xs 3 года назад

      @@anthonywritescode Okay, thanks for quick response

  • @talhajavaid3020
    @talhajavaid3020 4 года назад +1

    Thanks ! very helpful

  • @ingtob
    @ingtob 5 месяцев назад

    What is the purpose of subparsers? Are they simply grouping arguments?

    • @anthonywritescode
      @anthonywritescode  5 месяцев назад +1

      they are subcommands. think like "git status" vs "git commit"

  • @vitamind_forme3850
    @vitamind_forme3850 2 года назад

    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

    • @anthonywritescode
      @anthonywritescode  2 года назад

      you must have forgotten the `= None` part ? github.com/anthonywritescode/explains/blob/32d27aa9480bf67492d31d614bf5fae3746bfff2/sample_code/ep044/rev01/t.py#L7

  • @gatorduck11
    @gatorduck11 Год назад

    This made argparse click and make sense

  • @sadiqrahmati5276
    @sadiqrahmati5276 3 года назад +1

    Awsome channel! ty.

  • @ahmedahmedx9600
    @ahmedahmedx9600 Год назад

    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.

    • @anthonywritescode
      @anthonywritescode  Год назад

      they'll all end up in the args object returned from parse_args

  • @talalkalai8748
    @talalkalai8748 2 года назад

    any thoughts about 'click' pkg?

    • @anthonywritescode
      @anthonywritescode  2 года назад

      yeah, it's nice, but imo over-complicated, slightly-too-magical, needs to be installed, and argparse works well enough

    • @talalkalai8748
      @talalkalai8748 2 года назад +1

      @@anthonywritescode what about "adding autocomplete" video?

    • @anthonywritescode
      @anthonywritescode  2 года назад

      I haven't found a good solution for python that isn't hella slow beyond hand writing the bash / zsh outputs once

  • @yoav_caspi
    @yoav_caspi 4 года назад +1

    great video!

  • @yomajo
    @yomajo Год назад

    why exit(main()) instead of main()?

    • @anthonywritescode
      @anthonywritescode  Год назад

      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

    • @yomajo
      @yomajo Год назад

      @@anthonywritescode Sorry, might have skipped it. Thanks, added to watch list. 🙏

  • @skarfie123
    @skarfie123 Год назад

    It's not in standard library but I highly recommend typer over argparse.