Understanding Python: Argparse CLI

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024
  • In this video, I go over how to write a CLI using argparse.
    Included in the lesson is how to create a parse, add arguments, and parse the arguments. Additionally, I show a number of features argparse offers for arguments and options.
    As always, if you have any questions or suggestions for future videos, please leave a comment down below.
    Follow me on twitter: / jakejcallahan
    Source: github.com/Jac...
    Timelapse music: 失望した by Eva
    Link: • EVA - 失望した [Synthwave]...
    Outro music: Elix by Synthness
    Link: • Synthness - Elix ★ No ...
  • НаукаНаука

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

  • @victorhorowitz1519
    @victorhorowitz1519 Год назад +1

    Watched a bunch of arg parse tutorials prior to this one but yours is the best without question. You explained all the concepts I was looking to learn like actions, and dest so well. Thank you!

  • @topcivilian
    @topcivilian Год назад +1

    The google algorithm brought me here...
    Instant "like" and "subscribe"; this content is absolute gold.
    Clear explanations with a low-key voice at a controlled pace .
    This channel is criminally underrated, hope you get a million subs

  • @talgatm5398
    @talgatm5398 3 года назад +6

    Thank you so much! Manual CLI Processing & Argparse CLI are awesome tutorials! I like how you explained these concepts through building pizza. After these tutorials I was able to go through Python documentation for Argparse and understand what is written there :) And also I like how calm your voice is, it helps me lose my anxiety when I sit down to learn new coding concept :) Thanks for keeping it simple & clean!

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

      Your comment just made my day! I appreciate the feedback!

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

    One of the best argparse Vedios, so well explained. Loved the pace. Each concept. Of type of args, args as flags are superbly explained.

  • @MA-qr6vh
    @MA-qr6vh Год назад

    Extremely useful! Thank you.

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

    10:42 you can also set up VSCode to use the popular Python formatter called Black to automatically format the file on save so the line length stays under control as you work

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

      You sure can. I typically keep mine to trigger on command and/or in pre-commit hooks,

  • @jpcoffelt
    @jpcoffelt 10 месяцев назад

    Thanks for sharing (and convincing me to integrate ChatGPT)!

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

    Great tutorial, thanks.

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

    Great tutorial, this is a clear and very straight forward video that I have come across. GREAT!!!

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

    That's a very well thought and well executed tutorial. Much appreciated.

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

    Great, constructive, informative tutorial!
    Chioce tricks for parser is really helpful.
    Thank you!

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

    Thanks for the explanation! It helped a lot :)

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

    argparse.add_argument is one of a few examples where one should use long line formatting, with columns of matching parameters aligned. Think of dozens of parameters ...

  • @user-jl8wm8rq4e
    @user-jl8wm8rq4e Месяц назад

    Cool, thanks

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

    You should have covered how to include default settings in help texts.

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

    Really Nice.

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

    Best explanation! Thank you,

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

    so much value

  • @tsankailieva
    @tsankailieva Год назад +1

    Thanks a ton for the video. I'm trying to get argument "price

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

      So for this you can implement a custom "type". argparse's type field can accept a function. That function should only accept a single string value being passed in. This value will be what the user passes.
      In your function, you can convert the value to an integer and check that it is less than 20. If so, return it. If not, raise a argparse.ArgumentTypeError(your_message).
      So your argparse option should look like
      ..., type=int_lt_20, ...)

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

    Already commented on the other videos but I´ll do it for the networking effect :D
    Thanks so much for these videos, it´s super easy to follow along because you are calm, speak clearly and obviously know what you´re doing :)
    All of the videos have already helped me a lot as I am a super beginner with quite an ambitious project going on. Reading library documentation is like a big adventure :D
    Additionally I have a question:
    Is it possible to build something like food_builder (derived from your pizza builder) which
    1. does not have to be called with "python food_builder.py" only with "food_builder ..."
    2. has subcommands like:
    food_builder pizza [arguments]
    food_builder pasta [arguments]
    Your answers would be greatly appreciated

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

      Thank you! I don't get much time to make videos, but hopefully I'll be continuing soon.
      Regarding your question, absolutely! You'll want to install your project with something like pip. For that, you will likely need to go the setup.py route, setting your script's CLI as the entrypoint.
      For subcommands, you can do that quite easily with click's groups.
      If you go to my GitHub page and look at the Broker repository, you will find a bunch of tricks around making a CLI like that.
      Don't hesitate to ask questions along the way!