Sunday, April 16, 2017

How to make a dotnet CLI Tool

Good news, everyone! It is remarkably easy to make a new dotnet CLI (Command Line Interface) tool! I recently created a CLI tool for one of my new projects, Tact.NET RPC, and in this post I will be referencing that project as my example.

The Basics

Step 1) Create your CLI Console App

All you have to do is...

  1. Create a normal .NET Core Console App.
    • NOTE: Currently, the dotnet CLI only supports netcoreapp1.0
  2. Rename the assembly to be prefixed with "dotnet-"
  3. dotnet pack the project and put the package in your local NuGet package source

...that is it! It is literally that easy to create your CLI tool!

Recommendation: use Microsoft.Extensions.Configuration to parse your command line arguments in a standard way.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <AssemblyName>dotnet-tactrpcgen</AssemblyName>
    <PackageId>Tact.Rpc.Generator</PackageId>
    <Version>1.0.3</Version>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.1.1" />
  </ItemGroup>
</Project>

Step 2) Consume your CLI Tool NuGet Package

Edit your csproj file and add a DotNetCliToolReference element that references your package.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Tact.Rpc.Generator" Version="1.0.3" />
  </ItemGroup>
</Project>

Now you are able to invoke your console app via the dotnet command line whenever it is executed in the same path as the csproj.

That's it; it really is that easy!

Development Tips

Here is a simple way to automate the creation and consumption of your CLI tool NuGet package during development.

Create a local package directory

I recommend that you create a packages folder at the root of your solution along with a NuGet.config file; this will allow you to automatically build and consume NuGet packages from this location.

<configuration>
  <packageSources>
    <add key="LocalPackages" value="packages" />
  </packageSources>
</configuration>

Pack on post build

Go back and update your CLI tool's csproj to include a post build step that packs your project and places the output in your package folder. (There is a csproj GeneratePackageOnBuild setting, however at this time I do not believe that you can customize the output directory, so instead I recommend using a post build step.)

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <AssemblyName>dotnet-tactrpcgen</AssemblyName>
    <PackageId>Tact.Rpc.Generator</PackageId>
    <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
    <PostBuildEvent>
      dotnet pack $(SolutionDir)rpc\src\Tact.Rpc.Generator -o $(SolutionDir)packages --no-build
    </PostBuildEvent>
    <Version>1.0.3</Version>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.1.1" />
  </ItemGroup>
</Project>

Execute on pre build

Obviously this is a completely optional step that should only be used when necessary; in the case of Tact.NET RPC, the active code generation is powered by the CLI tool, and thus it needs to run every build.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
    <PreBuildEvent>
      cd $(SolutionDir)rpc\demo\Demo.Rpc
      dotnet tactrpcgen
    </PreBuildEvent>
  </PropertyGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Tact.Rpc.Generator" Version="1.0.3" />
  </ItemGroup>
</Project>

Increment version to update

NOTE: Despite updating the your local package directory each build, the package will actually be cached in your global NuGet cache, and thus updates will not be picked up unless you increment the version number.

Enjoy,
Tom

29 comments:

  1. The code generator module utilizes the "parse information" to build the test saddle source code.
    mélybölcsős fuvarozás Europa-Road Kft.

    ReplyDelete
  2. Pakistan no.1 Article in my softcrackersstore.com site Now just 1 click to download!
    GoodSync Crack
    Brave Browser Crack
    VSDC Video Editor Pro Crack

    ReplyDelete
  3. I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me.
    ClamWin Antivirushttps://softeir.com/dvdfab-passkey

    ReplyDelete
  4. Gemini Exchange is a cryptocurrency trading service that combines an exchange, a fiduciary custodial service for digital means, and an advanced trading platform.
    uniswap exchange |
    uniswap exchange |
    gemini exchange |
    gemini exchange |
    log in att email |
    log in att email |
    coinbase exchange |
    uniswap exchange |

    ReplyDelete
  5. Thanks for the great post you posted. I like the way you describe the unique content. The points you raise are valid and reasonable. I am a tech support expert telling you about.
    best cryptocurrency exchange|
    kraken exchange|
    uniswap exchange |
    crypto.com exchange|

    ReplyDelete
  6. Thanks for the great post you posted. I like the way you describe the unique content. The points you raise are valid and reasonable. I am a tech support expert telling you about.
    crypto wallet|
    exodus wallet|
    metamask wallet|
    trezor wallet|
    phantom wallet|

    ReplyDelete
  7. I am feeling very greatfull to go to here, thank you for you a lot for sharing the precious information
    exodus wallet |
    robinhood wallet |
    coinsmart login

    ReplyDelete
  8. Crypto Exchange is one of the best platforms for you if you are looking for a place to invest in the overgrowing wave of online currencies. crypto.com exchange | coinbase wallet | trust wallet |

    ReplyDelete
  9. Cryptocurrencies have evolved and managed to leave a mark in the next chapter of the financial industry along with tech improvisations.
    binance us login |
    ledger wallet |
    kucoin login

    ReplyDelete
  10. I am feeling very greatfull to go to here, thank you for you a lot for sharing the precious information.
    coinsmart login |
    robinhood wallet |
    exodus wallet

    ReplyDelete
  11. Cryptocurrencies have evolved and managed to leave a mark in the next chapter of the financial industry along with tech improvisations. It has been quite the journey for the digital currencies from no financial worth to hundreds of dollars’ worth. robinhood wallet | robinhood crypto wallet

    ReplyDelete
  12. MetàMask Lògin is a cryptocurrency handbag which can be used on the Chrome, Firefox as well as Brave browsers. It’s likewise a browser extension. metamask login | metamask wallet | metamask extension |

    ReplyDelete
  13. Finances in our world were given an online existence and backed with digital cryptography, it is now known as cryptocurrency. To make crypto easily accessible to the entire world, crypto exchange platforms came to life along with crypto wallets to provide a safe space for your crypto funds.
    Coinbase.com login |
    Pro.coinbase.com Login |
    Coinbase Wallet

    ReplyDelete
  14. This is one of the largest crypto exchanges that are being used by a lot of Americans to trade with digital currency. On the Coinbase login exchange, you cannot only trade with crypto but also store the purchased crypto by signing up for a Coinbase wallet. coinbase down | crypto.com login | pancake swap

    ReplyDelete
  15. Crypto.com log in account on Apple devices. You can easily access the Crypto.com account on your iOS devices by getting its mobile application from the App Store. Once you get the mobile app, you will be able to log in to your Crypto.
    crypto.com exchange|
    crypto.com login|
    crypto.com login

    ReplyDelete
  16. Binance is a cryptocurrency exchange that provides a platform for trading various cryptocurrencies. In addition, it provides a crypto wallet for its traders, where they can store their electronic funds.
    binance us login
    The exchange also has supporting programs that help traders make investment decisions. In addition, Binance has its blockchain-based token, Binance Coin.
    binance.com login

    ReplyDelete
  17. These are genuinely fantastic ideas about blogging really. You have touched some very nice points here. Please keep up this good writing.

    B Sc 1st NC Result

    ReplyDelete
  18. I guess I am the only one who came here to share my very own experience. Guess what!? I am using my laptop for almost the past 2 years, but I had no idea of solving some basic issues. I do not know how to Crack Softwares Free Download But thankfully, I recently visited a website named ProCrackHere
    Wing Ftp Server Corporate Crack
    Mathworks Matlab Crack

    ReplyDelete
  19. The Metamask login account was created out of the need to create more secure and usable Ethereum-based websites. To be specific, it clearly handles account management and connects the user to the Binance blockchain. It clearly supports the Chrome, Brave along with the Safari browsers.
    metamask |

    Gemini is a popular cryptocurrency exchange platform that helps you to buy Bitcoin, Ether, and other cryptocurrencies easily and securely. On this platform, there are several cryptocurrencies available like Bitcoin, Ether, Cardano, Dogecoin, XRP, Polkadot, and so on.
    gemini |

    MetaMask login is one of the most popular and trending web browser extensions that are used in storing Ethereum and ERC-20 tokens.. MetaMask is free of cost and a secure way to invest in cryptocurrency.MataMask operates on the concept of blockchain where you can uniquely record and transfer information into the network in a secure way.
    metamask login |

    ReplyDelete
  20. Thanks for the great post you posted. I like the way you describe the unique content. The points you raise are valid and reasonable. I am a tech support expert telling you about.
    kraken login
    phantom wallet

    ReplyDelete

  21. MetaMask allows its customers to access the wallet via its browser extension and mobile app. You can use the wallet on your mobile or computer or a separate device.
    MetaMask Login |
    MetaMask Login |
    uphold login

    ReplyDelete
  22. If you have the details that you need to fill up your Uphold sign-up form then you need to jump to the next section to start the sign-up and login process.
    uphold login
    Kraken is considered one of the most preferred crypto exchanges for Bitcoin trading. Trading Bitcoin kraken is safer on this exchange.
    kraken login

    ReplyDelete

Real Time Web Analytics