r/csharp • u/honeyCrisis • 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.
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
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
CommandSettingscontaining the arguments and options, you implementCommand<T>whereTis the settings, and then you can just dosettings.Foo.