python argparse tutorial (beginner - intermediate) anthony explains

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ต.ค. 2024

ความคิดเห็น • 48

  • @adwaithrajesh596
    @adwaithrajesh596 3 ปีที่แล้ว +12

    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?"
    TH-cam: "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.

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว

      yeah! append is super useful for repeated arguments

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars 2 ปีที่แล้ว +2

    wow, just wow. Thank you and subbed :)

  • @gatorduck11
    @gatorduck11 ปีที่แล้ว

    This made argparse click and make sense

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

  • @n.a.s1096
    @n.a.s1096 ปีที่แล้ว

    Thank you, Anthony.

  • @talhajavaid3020
    @talhajavaid3020 3 ปีที่แล้ว +1

    Thanks ! very helpful

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

  • @sadiqrahmati5276
    @sadiqrahmati5276 3 ปีที่แล้ว +1

    Awsome channel! ty.

  • @DanielGalasso
    @DanielGalasso ปีที่แล้ว

    Excellent tutorial

  • @yoav_caspi
    @yoav_caspi 4 ปีที่แล้ว +1

    great video!

  • @muddasirkhan805
    @muddasirkhan805 2 ปีที่แล้ว +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  2 ปีที่แล้ว +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 2 ปีที่แล้ว

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

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

  • @ingtob
    @ingtob 5 หลายเดือนก่อน

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

    • @anthonywritescode
      @anthonywritescode  4 หลายเดือนก่อน +1

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

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

  • @DennisZIyanChen
    @DennisZIyanChen 3 ปีที่แล้ว +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  3 ปีที่แล้ว +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: th-cam.com/video/WyR1hAGmR3g/w-d-xo.html -- entirely in the terminal!

  • @jake115577
    @jake115577 หลายเดือนก่อน

    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  หลายเดือนก่อน +1

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

    • @jake115577
      @jake115577 หลายเดือนก่อน

      @anthonywritescode thank you!!

  • @tsankailieva
    @tsankailieva ปีที่แล้ว

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

    • @anthonywritescode
      @anthonywritescode  ปีที่แล้ว

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

  • @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: th-cam.com/video/ZbeSPc5wL0g/w-d-xo.html

    • @yomajo
      @yomajo ปีที่แล้ว

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

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

  • @skarfie123
    @skarfie123 ปีที่แล้ว

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