Rust Command Line Argument Parsing (A Better Way With Clap)

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 มิ.ย. 2024
  • A quick tutorial on a better way to parse command line arguments in Rust, using Clap.
    00:00 Introduction
    00:39 Without Clap
    01:10 Clap Overview
    01:36 Parser
    02:18 main.rs
    02:40 Testing A Simpler Parser
    02:48 A More Complex Parser
    04:47 Testing A Parser With Subcommands
    05:11 Reading Values From The Parser
    ---
    Stuff I use to make these videos - I absolutely love all of these products. Using these links is an easy way to support the channel, thank you so much if you do so!!!
    Camera: Canon EOS R5 amzn.to/3CCrxzl
    Monitor: Dell U4914DW 49in amzn.to/3MJV1jx
    Lens: Sigma 24mm f/1.4 DG HSM Art for Canon EF amzn.to/3hZ10mz
    SSD for Video Editing: VectoTech Rapid 8TB amzn.to/3hXz9TM
    Microphone: Rode NT1-A amzn.to/3vWM4gL
    Microphone Interface: Focusrite Clarett+ 2Pre amzn.to/3J5dy7S
    Tripod: JOBY GorillaPod 5K amzn.to/3JaPxMA
    Keyboard: Redragon Mechanical Gaming Keyboard amzn.to/3I1A7ZD
    Mouse: Razer DeathAdder amzn.to/3J9fYCf
    Computer: 2021 Macbook Pro amzn.to/3J7FXtW
    Caffeine: High Brew Cold Brew Coffee amzn.to/3hXyx0q
    More Caffeine: Monster Energy Juice, Pipeline Punch amzn.to/3Czmfox
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @barlingo7060
    @barlingo7060 ปีที่แล้ว +51

    Great tutorial,
    One minor thing it's that you should specify that at the toml file you need to specify the derive feature to be able to use it:
    [dependencies]
    clap = { version = "3.2.8", features = ["derive"] }

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

      thanks and great point! I should have definitely included this.

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

      @@codetothemoon Should honestly have showed the entire toml.
      And I can't figure out what is the difference between the macros "clap" (which you used) and "command" (which the docs suggest using)

    • @lauralegerofficial
      @lauralegerofficial 6 หลายเดือนก่อน +1

      Thank you! I was wondering why my compiler was screaming when I did everything he said.

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

    What perfect timing. I've been working on a CLI application and basing my argument parsing off one of the projects in the rust book. This looks much nicer to work with, even if I don't need nested arguments!

    • @codetothemoon
      @codetothemoon  2 ปีที่แล้ว

      I agree, it's hard to make a case for not using Clap, even for smaller projects!

  • @schlopping
    @schlopping ปีที่แล้ว +5

    A crate "clap" for a language where the mascot is a crab is hilarious LOL

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

    Excellent - thank you! I couldn't for the life of me figure out how to implement subcommands using the derive framework, and the builder method (the ony example I could find) produces code I find dense and hard to read. Perfect.

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

    Great tutorial & crate. Thanks for explanations!

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

      Thanks, glad you found it valuable!

  • @learntry9074
    @learntry9074 5 หลายเดือนก่อน +1

    Thank you. Your all vids are gem.

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

      thanks for the kind words!

  • @oglothenerd
    @oglothenerd 11 หลายเดือนก่อน +1

    This tutorial is a gem in the dust! I love how you are the one who actually made a useful tutorial! Thank you!

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

    Thxs for this explanation, it clarified me a lot how to define a CLI in rust. I would like to know how we can have bi-directional communication with the rust app and the command line. Could you please do an example of stdin / stdout with rust?

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

    This was an amazing video, short sweet and to the point. Very informative as well. I would say for future reference, you should probably show the set up of the project, even if it's brief. For example, someone really new to rust might not know that you actually have to add "features = ["derive"]" to their cargo.toml to work correctly. Other than that, great video!

    • @codetothemoon
      @codetothemoon  2 ปีที่แล้ว

      Thanks Cheryl, glad you found it valuable. Great point about covering project setup - feature=["derive"] is a big gotcha!

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

    The sped up typing looks really satisfying, though I bet it was alot of work to do in video editing

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

      Yeah it definitely makes editing take longer, but until I can type at 200wpm I don't want to make everyone sit there and watch me type 😎

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

    Hi, thanks for this content. Really really helpful for me.
    I've been following your implementation of the Rustflix project and I'd like to serialize to JSON the values of `CreateView` or `UpdateVideo` structs (just to mention an example). I have tried several ways but have had no luck. Any hint?
    Thanks in advance :)

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

    I love this crate.

  • @ashutosh.sharma
    @ashutosh.sharma 5 หลายเดือนก่อน +1

    I have a question: why create `struct UserCommand`? why not just create an make UserCommand as an enum. That extra stuct then enum step seems unnecessary. Please let me know your thoughts

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

    Greate tutorial.
    For clap - can you elaborate on when to use the derive mechanism and when to use app creation?

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

      Thanks! By app creation do you mean the builder API? If so my understanding is that it's mostly a personal preference thing, though it appears they have very slight differences in capability docs.rs/clap/latest/clap/#selecting-an-api

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

      @@codetothemoon Yes, that's what I meant. Thanks!

  • @HAL-9000-
    @HAL-9000- 2 ปีที่แล้ว +2

    Nice 👌

  • @beholdthegoober
    @beholdthegoober 6 วันที่ผ่านมา

    Any way to make multiple names for the same command, like a semantics thing? say i wanted to create a file and wanted to accept if the command was either 'create' or 'touch'. maybe not important but would like to do it for fun or personal preference..

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

    How do you allow for empty values? I want to use a single letter flag set to true when present and false when absent, but also allow for -z true and -z false

  • @adeoyeadefemio.8859
    @adeoyeadefemio.8859 ปีที่แล้ว +2

    Great tutorial. Could you please share the link to the code repository :)

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

      Sure here it is! github.com/Me163/youtube/tree/main/Rustflix

    • @adeoyeadefemio.8859
      @adeoyeadefemio.8859 ปีที่แล้ว

      @@codetothemoon thanks

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

    but now, how to get the values?

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

      the struct returned by RustflixArgs::parse() should be the struct you applied #[derive(Parser)] to, so you can just pull the values from there! I definitely glossed over this a bit too quickly at the end of the video.

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

    How to get help menu if no argument is passed

    • @OdilonNelson
      @OdilonNelson 5 หลายเดือนก่อน +1

      Annotate your struct with
      #[command(arg_required_else_help = true)]

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

    Also, would be good if you can provide at least a Github gist of your codes lol, that should be basic.

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

    This was hard to follow, as you always scroll away, even if there is no need to.
    Otherwise solid content.

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

    You kinda look like a blond Elon Musk

    • @codetothemoon
      @codetothemoon  3 หลายเดือนก่อน +1

      hah! Sadly, I haven't slung quite as many things in orbit

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

      I can’t unsee it