Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
洪 民憙 (Hong Minhee) ( hongminhee@lemmy.ml )  to Programming@programming.devEnglish · 1 year ago

Stop writing CLI validation. Parse it right the first time.

hackers.pub

external-link
message-square
11
link
fedilink
  • cross-posted to:
  • typescript@programming.dev
  • programming@lemmy.ml
75
external-link

Stop writing CLI validation. Parse it right the first time.

hackers.pub

洪 民憙 (Hong Minhee) ( hongminhee@lemmy.ml )  to Programming@programming.devEnglish · 1 year ago
message-square
11
link
fedilink
  • cross-posted to:
  • typescript@programming.dev
  • programming@lemmy.ml
This post introduces Optique, a new library created to address the pervasive problem of repetitive and often messy validation code in CLI tools. The author was motivated by the observation that nearly every CLI tool reinvents the wheel with similar validation patterns for dependent options, mutually exclusive options, and environment-specific requirements. Optique leverages parser combinators and TypeScript's type inference to ensure that CLI arguments are parsed directly into valid configurations, eliminating the need for manual validation. By describing the desired CLI configuration with Optique, TypeScript automatically infers the types and constraints, catching potential bugs at compile time. The author shares their experience of deleting large chunks of validation code and simplifying refactoring tasks. Optique aims to provide a more robust and maintainable approach to CLI argument parsing, potentially saving developers from writing the same validation logic repeatedly.
alert-triangle
You must log in or register to comment.
  • thingsiplay ( thingsiplay@beehaw.org ) 
    link
    fedilink
    arrow-up
    18
    ·
    1 year ago

    https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/ - Edit: Ah I see, the author of this article is pointing to this older article too and admits being influenced by it. So never mind.

  • Asetru ( Asetru@feddit.org ) 
    link
    fedilink
    arrow-up
    15
    ·
    1 year ago

    The or() combinator means exactly one succeeds.

    Using “or” to define a function that does “xor”… Did that guy never hear about formal logic? That’s, like, first or second semester stuff…

    Here’s the thing: I don’t have a CS degree.

    sigh

  • Onno (VK6FLAB) ( vk6flab@lemmy.radio ) 
    link
    fedilink
    arrow-up
    8
    ·
    1 year ago

    Oh boy … very cool.

    Now how do I do this in bash?

    • kibiz0r ( kibiz0r@midwest.social ) 
      link
      fedilink
      English
      arrow-up
      7
      ·
      1 year ago

      Not exactly an answer, but I’ll take the opportunity to point out that Bun has a shell feature which makes it easy to mix and match JS and Bash in the same script, and it provides a compatibility layer for Windows users so that you don’t have to worry about platform differences in shell capabilities. https://bun.sh/guides/runtime/shell

  • TehPers ( TehPers@beehaw.org ) 
    link
    fedilink
    English
    arrow-up
    3
    ·
    1 year ago

    I like the concept, and it’s great in TS. Unfortunately, not as doable in other languages.

    I’m a bit curious if it’s possible to extend clap to do this in Rust though (specifically mutually-exclusive arg groups).

    • verstra ( verstra@programming.dev ) 
      link
      fedilink
      arrow-up
      7
      ·
      1 year ago

      clap already supports all this: https://docs.rs/clap/latest/clap/struct.Arg.html#method.conflicts_with It’s just a great library, having you could think of and applying the same parse-don’t-validate mentality.

      • TehPers ( TehPers@beehaw.org ) 
        link
        fedilink
        English
        arrow-up
        7
        ·
        1 year ago

        This doesn’t represent the mutual exclusivity through the type system (which is what the article is all about).

        I love clap and I use it a lot, but the only way to represent the exclusivity through the type system in Rust is through an enum.

        • ExFed ( ExFed@programming.dev ) 
          link
          fedilink
          arrow-up
          4
          ·
          1 year ago

          Agreed. As nice as clap is, it’s not a combinator. Parser combinators have a the really nice feature of sharing the same “shape” as the data they parse, which makes them trivial to generate from a schema … or to just use them to represent your schema in the first place ;) .

    • Ephera ( Ephera@lemmy.ml ) 
      link
      fedilink
      English
      arrow-up
      3
      ·
      1 year ago

      Clap has dependent options and mutually-exclusive argument groups built-in: https://docs.rs/clap/latest/clap/_derive/_tutorial/index.html#argument-relations

      For the environment-specific requirements, you can use compiler feature flags…

      • TehPers ( TehPers@beehaw.org ) 
        link
        fedilink
        English
        arrow-up
        6
        ·
        1 year ago

        Mentioned this to the other commenter, but this doesn’t use the type system to enforce the mutual exclusivity constraint. In Rust, the main way to do that via the type system is through enums.

        • Ephera ( Ephera@lemmy.ml ) 
          link
          fedilink
          English
          arrow-up
          3
          ·
          1 year ago

          Ah, fair enough. Not sure how to do that then.

          I was gonna say, I feel like the current method does a good enough job documenting that validation has happened, but I guess you do want it reflected in the structure of the type, so that the code that takes the information from the struct can safely make the assumption that some of the options don’t exist. And then, yeah, it would be nice to not need a separate parsing step for that.

Programming@programming.dev

programming@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programming@programming.dev

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 53 users / day
  • 632 users / week
  • 1.87K users / month
  • 4.56K users / 6 months
  • 452 local subscribers
  • 29.5K subscribers
  • 3.17K Posts
  • 24.4K Comments
  • Modlog
  • mods:
  • snowe ( snowe@programming.dev ) 
  • Ategon ( Ategon@programming.dev ) 
  • UlrikHD ( UlrikHD@programming.dev ) 
  • bugsmith ( bugsmith@programming.dev ) 
  • Spyro ( Spyro@programming.dev ) 
  • BE: 0.19.21
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code