This is Best Way To Create CLI Apps in .NET

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ธ.ค. 2023
  • Use code CLEAN20 and get 20% off the brand new "Deep Dive in Clean Architecture" course on Dometrain: dometrain.com/course/deep-div...
    Get the source code: mailchi.mp/dometrain/4khfrbjhchc
    Become a Patreon and get special perks: / nickchapsas
    Hello everybody, I'm Nick, and in this video, I will introduce you to what I think is the easiest way to create elegant CLI (Command Line Interface) tools in .NET using C#. There are many ways to achieve this in .NET but by far my favourite is using a Nuget package called Cocona.
    Give Cocona a star on GitHub: github.com/mayuki/Cocona
    Workshops: bit.ly/nickworkshops
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: github.com/Elfocrash
    Follow me on Twitter: / nickchapsas
    Connect on LinkedIn: / nick-chapsas
    Keep coding merch: keepcoding.shop
    #csharp #dotnet

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

  • @MB-Kajtech
    @MB-Kajtech 7 หลายเดือนก่อน +17

    Perfect timing. Was just going to create a CLI tool.

  • @woocaschnowak
    @woocaschnowak 7 หลายเดือนก่อน +51

    Nice lib. But what I loved is that you mentioned that you wouldn't/shouldn't put commands into program.cs. I recently talked to a guy that said he doesn't like minimal apis, cause the program.cs is a mess. Cause all he saw were those presentations where it was "one file API" and didn't think about splitting it. Here's similar approach so mentioning the splitting may ease adoption for some people.

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

      I'm one of those guys - I was always aware that we could split onto multiple files, but seeing all the examples and talks about minimal APIs in a single-file scope clearly makes it seem like these talks are being targeted at other framework/language devs (e.g. Node.js devs) and not .NET devs (which is, of course, fine).
      All in all, they have their place and their use, but I am still not captivated by the beauty of a single-file use, even if it's for a very simple API structure. I'm keeping an open mind and giving me time to get used to it, though (because I used Carter in the past and loved it, I'm expecting the same for these sort of minimal API approaches).

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

      Well, minimal APIs can actually reside in a separate file other than Program.cs
      Also there are Visual Studio scaffolders which can do this for you.

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

      The beauty is that you can have a really simple solution to eg. attract non .net developers: easy to learn. But both in Node and c# you can of course split things them in any way you want if you are scaling out to more features.

  • @user-zf2xe8nm5f
    @user-zf2xe8nm5f 7 หลายเดือนก่อน +1

    massive. so pleased to know about this. I built a testing tool and the navigation here will totally streamline the workflow and extensibility.

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

    I've attempted to make rudimentary CLI programs approximating this structure a whole bunch of times over the years, never quite got it right. Glad to see there's a library that already has me covered.

  • @carlosmunozrodriguez
    @carlosmunozrodriguez 7 หลายเดือนก่อน +5

    I'm currently working on a CLI myself and setting up DI is challenging but doable. Gonna give Cocona a try.

  • @alvarezbarbosa12
    @alvarezbarbosa12 7 หลายเดือนก่อน +2

    Nick seems to be always make relevant videos, appreciate it a lot!

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

    Happy New Year, Nick! And congratulations on the great content that you produce. Wish you a great 2024.

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

    Wow! As usual thanks for sharing! That looks pretty powerful!

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

    I was just thinking about making such framework by myself. Glad it already exists, saves me a lot of time.

  • @jonathansaindon788
    @jonathansaindon788 7 หลายเดือนก่อน +1

    This is so cool! Thanks Nick for a that little gem!

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

    Amazing!!! I definitely will use this lib in my future applications

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

    This is a great package. This week I added a app builder to a CLI project I have to assist with some integration tests, If I had seen this before hand, I would have used this package.

  • @yunietpiloto4425
    @yunietpiloto4425 7 หลายเดือนก่อน +2

    This project is really nice, I’ve been following for a while now. I think a good point to make is that people should be careful when injecting services since the console process runs as a singleton. Thanks for sharing

  • @tomerza
    @tomerza 7 หลายเดือนก่อน +1

    Thanks,
    I usually use it for small tools for me or some team-mates. Thats why I never thought of even doing unit-tests for them :-)

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

    I really like this library and am using it when I need to create some CLI console tools

  • @infanatic
    @infanatic 7 หลายเดือนก่อน +19

    I feel like spectreconsole library is a bit more advanced in feature set.tried cocona in a PoC and liked it but spectreconsole felt more mature

  • @alexeydimov4423
    @alexeydimov4423 7 หลายเดือนก่อน +1

    That's a nice lib! I built a simmilar one around a year ago, with a difference that there were a class-based commands with tagging Attributes for arguments injecting into a properties, and to define names, descriptions, etc, but I feel like MinimalAPI approach could be better for some tasks

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

    Cool video. Thanks Nick!

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

    really nice. thanks Nick

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

    Thanks, I will use it to build a tool to create fake data to help with local development of microservice. You are awesome nick. Keep going.

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

    Feels like if you could take the minimal api architecture and rip out the exposed port you’d have a solid structure for this. Especially the slim web builder sans the serving of course

  • @marksouzacosta
    @marksouzacosta 7 หลายเดือนก่อน +2

    I use the Command Dot Net. Very good CLI framework.

  • @codingbloke
    @codingbloke 7 หลายเดือนก่อน +8

    Thanks. I'll take look at this. I've been using the CommandLine package which does a good job of defining verb + verb specific options style CLI (like git). Although not show in the video I'm gonna guess a public method can be decorated so that the "command" as a verb not an option. I'm a little surprised that isn't the default behaviour but may be there is config that will make it the default.

    • @user-dq7vo6dy7g
      @user-dq7vo6dy7g 6 หลายเดือนก่อน

      We recently switched to the System.CommandLine Package from McMastersCommandLineUtils. I am totally satisfied with that package so far and might soon use it for a new project.

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

    Loved this Cocona stuff!! WAY more useful than those old args[] trash!

  • @pdevito
    @pdevito 7 หลายเดือนก่อน +1

    I’ve liked working with Sprache more personally, but fun watch regardless!

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

    I write command line apps all the time, mostly for testing code parts in clean environment before using it in main project

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

    Going to have to give this library a go - looks like it will make everything a lot cleaner. Just need to work out how to get it to return an exit code so it will better integrate with scripts

  • @HaoNguyen-km9xj
    @HaoNguyen-km9xj 7 หลายเดือนก่อน

    Very nice, now may be say bye bye mono options :)

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

    Ah, this video could save me a couple of hours if was few days before. But anyway, this is very nice solution, will use it next time. Thanks!

  • @passionforsciencel5180
    @passionforsciencel5180 7 หลายเดือนก่อน +3

    Waiting for new web application builder benchmark ⌛, Good Luck 🌹

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

    my man’s timing is insane

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

    I've tried multiple CLI packages, and I prefer the Console.Spectre package. It was a better experience and more powerful for "pretty" console output. Perhaps you could try that one as well...

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

    nice lib and video

  • @dahahaka
    @dahahaka 7 หลายเดือนก่อน +1

    I'd have loved to hear a comparison with CliFx or Typin

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

    Hey Nick, as usual thanks for the tip. Do you know how to pass a cancellation token into the class based commands?

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

      It looks like you can pass the CoconaAppContext into the command method directly. I wonder why the application looks to be stuck if you inject it into the constructor

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

    I prefer spectre personally. They also provide alot of console interaction helpers like printing progress bars, tables, treeview + prompting for input values including selection of values etc

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

    How do you show the project and version in DOS?

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

    Nice

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

    I like commandlineparser/commandline

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

    I remember you've already done a video about Cocona 🤔

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

    "Cocona" is an excellent Pokemon Rangers reference

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

    I probably missed the part, but is it possible to have it accept commands WHILE the application is running?

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

    I love how you (intentionally?) say "option-null" for nullable parameters.

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

    I have a business use case that this library could work for.

  • @IvanMihaylov-um8xj
    @IvanMihaylov-um8xj 6 หลายเดือนก่อน

    I've tried using the library and I ran into a problem when inputting json strings. For example when i input {"name":"Something"} I get { name : Something }. Does anybody have an idea why is that happening

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

    What does this library offer in addition to system.commandline?

    • @palecskoda
      @palecskoda 6 หลายเดือนก่อน +2

      Was wondering about that too. System.CommandLine is still in preview, but it is directly from Microsoft and looks very promising.

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

    which tool are you using for that colorful console?

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

      That's "Oh My Posh"

  • @ShadoFXPerino
    @ShadoFXPerino 7 หลายเดือนก่อน +1

    Bash and zsh completion support but no pwsh? Sad day for Powershell.

  • @_OsamaAmir
    @_OsamaAmir 7 หลายเดือนก่อน +2

    why there is space after the first two dashes, i.e dotnet run -- weather
    is it a quirk of the package or what, can someone kindly explain?

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

      It's to stop the dotnet command from potentially taking the args; it forces dotnet to pass the args to the Program.cs. If you don't do that and some of your args names clash with the dotnet cli, it could be a problem.

    • @ghidello
      @ghidello 7 หลายเดือนก่อน +5

      All arguments before "--" are for "dotnet run" while the others are passed to the application being run

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

      @@ghidello thanks for explaining

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

      @@ghidello same thing with npm run

    • @ghidello
      @ghidello 7 หลายเดือนก่อน +1

      @@SirusStarTVYep, it's pretty common in unix commands. You made me remember when I used to create a file named "-i" challenging people to delete it.

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

    Pretty cool, but the last update was on March 27, 2023 so it doesn't seem to be maintained as much; and it is targeting .NET 6 only (I know there is backward compatibility in .NET, but still). :(

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

    👍👍👍

  • @Alguem387
    @Alguem387 7 หลายเดือนก่อน +1

    Is this AOT compatible?

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

      Nope everything gets trimmed to oblivion. CLI without AOT makes me want to move to Rust or GO.

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

    can't we make our own command type instead of dotnet run? let's say I want to name it as Weatherize and the command's name will be Weatherize --city London?

    • @nickchapsas
      @nickchapsas  7 หลายเดือนก่อน +2

      Ofc you can. It’s only dotnet run because I’m running it while I’m working on it. Once you package it and you give it a name you can call it by its name

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

      @@nickchapsas thanks

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

    There is a terrible bug in the code in about 2 min into the video. The traditional greetings is written "Hello, world!". No exceptions! 😀

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

    Nesting subcommands using the class base approach is very clunky ATM.

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

    I'm coining a new term called "DI container obsession". While dependency injection itself is great, the use-case for DI containers is actually super narrow. The obsession with using them all over the place in .NET is really annoying.

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

    The source code link in the description leads to a 404

  • @skyrunner86
    @skyrunner86 11 วันที่ผ่านมา

    don't use this library. it only looks good on short youtube videos like this with hello world vibe. i actually tried to used it - you will not know what's wrong. the application will just stop/freeze and you will not know what happened. for example, if you won't configure a dependency injection for one of the classes, you will never know it explicitly. this library has a big issue with hiding runtime exceptions and giving you 0 feedback on what's wrong. don't use it for anything serious or you will waste hours of your time.