Understanding Python: Argparse CLI

Поділитися
Вставка
  • Опубліковано 27 сер 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

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

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

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

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

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

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

  • @jpcoffelt
    @jpcoffelt 10 місяців тому

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

  • @caribtree
    @caribtree Рік тому

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

  • @andygordon6880
    @andygordon6880 Рік тому

    Great tutorial, thanks.

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

  • @philtoa334
    @philtoa334 Рік тому

    Really Nice.

  • @joga2491
    @joga2491 2 роки тому

    Best explanation! Thank you,

  • @falklumo
    @falklumo Рік тому

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

  • @user-jl8wm8rq4e
    @user-jl8wm8rq4e Місяць тому

    Cool, thanks

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

  • @luigi9064
    @luigi9064 2 роки тому

    so much value

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