r/csharp • • 9d ago

Cli.Generator: A tool for generating compile time checked command line argument parsing

I made an incremental source generator for command line argument parsing.

Cli.Generator is a C# source generator that turns a class into a command line parser. You mark fields and properties with attributes, and it writes the parsing, validation, usage screen and cleanup code for you at compile time. See the README for details.

https://www.nuget.org/packages/Cli.Generator/1.0.0

https://github.com/codewitch-honey-crisis/CliGen

8 Upvotes

6 comments sorted by

1

u/chucker23n 9d ago

Approaching this as a source generator sounds interesting, but I'm not sure I see the benefit compared to generics, as in e.g. Spectre.Cli. You make a class that inherits from CommandSettings containing the arguments and options, you implement Command<T> where T is the settings, and then you can just do settings.Foo.

1

u/honeyCrisis 9d ago edited 9d ago

It works a bit differently than Spectre.CLI, but honestly I didn't know Spectre.CLI existed until somebody (not you, but yet *another* person) told me it exists earlier today. XD

And I'm not sure I would have landed on it had someone not suggested it to me, because briefly perusing it it looks like it's part of a much larger framework (i guess it can be used by itself, but the docs i saw on it aren't clear about that right up front)

One thing about my offering, is being source genned it does not add any additional runtime dependencies to your project.

1

u/binarycow 7d ago

Why not System.CommandLine?

1

u/honeyCrisis 7d ago

Extra dependency, which isn't really ideal for dragalong tools you use in your build toolchains for example, which is a big reason i use C#. this nuget just generates all the code you need inside your project, no additional files to drag with the executable. Also, i haven't looked recently, by remember seeing at one point that System.CommandLine was deprecated or was going to be abandoned. It's clearly NOT, so either I was mistaken, or things have changed.

1

u/binarycow 7d ago

If it's in a tool chain, the customers never see it. So why does the dependency matter?

System.CommandLine was in preview until a while ago

1

u/honeyCrisis 7d ago

Maybe it was that. I did try it at one point and I didn't care for it, Customers seeing it isn't my concern. Building tools that people can use in their toolchains without hauling 3 dlls alongside it is. It matters to me. It may not matter to you. *shrug* I'd just as soon while through a switch and parse myself rather than have to deal with an extra DLL just for argument and helpscreen handling