Belmansour / Brady | Building CLI Applications with C# and .NET | E-Book | www.sack.de
E-Book

E-Book, Englisch, 306 Seiten

Belmansour / Brady Building CLI Applications with C# and .NET

A step-by-step guide to developing cross-platform CLI apps—from coding and testing to deployment
1. Auflage 2025
ISBN: 978-1-83588-275-7
Verlag: De Gruyter
Format: EPUB
Kopierschutz: 0 - No protection

A step-by-step guide to developing cross-platform CLI apps—from coding and testing to deployment

E-Book, Englisch, 306 Seiten

ISBN: 978-1-83588-275-7
Verlag: De Gruyter
Format: EPUB
Kopierschutz: 0 - No protection



Developers and system administrators often face challenges like inefficient workflows, complex system operations, and the growing demand for robust automation tools. CLI applications provide a powerful solution by enhancing flexibility, efficiency, and productivity in various environments. This book will guide you through mastering the development of robust command-line tools using .NET.
Written by a Microsoft Azure MVP, the book's hands-on approach ensures practical experience with real-world projects. You'll start with an overview of foundational principles, essential concepts, and best practices for CLI application development. From there, you'll advance to creating interactive interfaces, integrating with external APIs and services, and implementing security measures to safeguard your applications. Each chapter will build progressively from basic to advanced topics.
Beyond development, you'll learn how to enhance application quality through testing, package for efficient distribution, and deploy effectively. The book also teaches strategies to optimize performance to ensure your applications run efficiently under heavy usage.
By the end of this book, you'll have gained a deep understanding of CLI application development with .NET to build modular, extensible, and easy-to-maintain applications.

Belmansour / Brady Building CLI Applications with C# and .NET jetzt bestellen!

Weitere Infos & Material


Table of Contents


Preface


Free Benefits with Your Book


Part 1: Getting Started with CLI Applications


1


Introduction to CLI Applications


A day in the life of an IT professional


Why care about CLI applications?


To CLI or not to CLI?


CLI applications as the building block for creating workstation profiles


Even heavy graphical applications have a CLI tool!


Even ChatGPT has a CLI!


Summary


2


Setting Up the Development Environment


Technical requirements


Installing Visual Studio Code


Installing the required extensions


Installing the .NET SDK


Installing and configuring Git


Summary


Your turn!


3


Basic Concepts of Console Applications in .NET


Technical requirements


Creating (and executing) a simple console application


Working with the System.Console class


Useful properties


Useful methods


Useful event


One more thing


Summary


Your turn!


Part 2: Foundations of Building CLI Applications


4


Command-Line Parsing


Technical requirements


Creating the console application


Parsing the arguments of a console application


From console to CLI – parsing the arguments using an existing library


Adding the root command


Adding the link command


About commands


Do all commands need to have a handler method?


Adding options to the link command


What other types of options can we use?


Getting help


Getting the application’s version number


Summary


Your turn!


Task #1 – Delete an existing bookmark


Task #2 – Update an existing bookmark


Task #3 – List all existing bookmarks


5


Input/Output and File Handling


Technical requirements


Controlling input values for an option


Required versus non-required options


What about arguments?


Setting a default value for an option


Controlling the allowed values for an option


Validating input values


Adding multiple elements in one go


Working with files passed in as options values


Summary


Your turn!


Task #1 – validating the format and the ability to access the input file


Task #2 – merging existing links from the input file


6


Error Handling and Logging


Technical requirements


Handling errors in CLI applications


Handling exceptions


Handling errors doesn’t necessarily mean handling exceptions


Handling program termination


Logging in CLI applications


Why JSON?


Why Serilog?


Accessing IServiceCollection


Adding Serilog to IServiceCollection


Adding (and configuring) the required Serilog sinks


Configuring sinks in appsettings.json


Let’s log something!


Closing and gracefully disposing of Serilog


Summary


Your turn!


Task #1 – Handling errors for the Import command


Task #2 – Logging errors to a file


Part 3: Advanced Topics in CLI Application Development


7


Interactive CLI Applications


Technical requirements


Building interactive command-line applications


Adding a FIGlet


Designing user-friendly CLI applications


Enhancing text display using markup


Offering choices to the user using selection prompts


Showing live progress of the export command


Displaying bookmarks in a tree view


To be or not to be interactive?


Summary


Your turn!


Task 1 – present a bookmark in a user-friendly way


Task 2 – change the category of a bookmark interactively


8


Building Modular and Extensible CLI Applications


Technical requirements


Step 1 – building a code map of the application


Using the Help menu to build the code map


Step 2 – deciding where to start


Step 3 – designing the project structure


Step 4 – refactoring the export command


Step 5 – applying the dependency inversion principle


Step 6 – refactoring the Program class


Step 7 – running the program


Taking refactoring to new heights


Updating the project structure


Summary


Your turn!


Task #1 – refactor the remaining commands


9


Working with External APIs and Services


Technical requirements


Why consume external APIs?


How to consume an external API


Benefits of using IHttpClientFactory


Bookmarkr: your bookmarks, anywhere!


The sync command


Registering the sync command


Running the program


Reducing the coupling between our application and the external dependency


About the Service Agent pattern


Implementing the Service Agent pattern


Rerunning the program


Summary


Your turn!


Task #1 – adding SQLite as a data store


Task #2 – retrieving the web page name based on its URL


Part 4: Testing and Deployment


10


Testing CLI Applications


Technical requirements


Why is testing so important?


Types of tests


About usability tests


The pyramid of (software) testing


What should we test?


What not to test


Testing is a safety net


When should we run tests?


Adding a test project to Bookmarkr


Structuring the test project


Code artifacts that should not be tested


Writing effective tests


Running our tests


Mocking external dependencies


The role of mocking


How to mock an external dependency


Mocking the BookmarkService service


Using the mock version of the BookmarkService service


Changes to the code must be made!


Going back to implementing the test cases


Internals visibility


Centralizing test initialization


How to hunt a bug


Summary


Your turn!


Task #1 – Write the required unit tests for the remaining functionalities


Task #2 – Write integration tests for the sync command


11


Packaging and Deployment


Technical requirements


A bit of terminology


Packaging and distribution options for CLI applications


Packaging and distributing a CLI application


Option #1 – as a .NET tool


Option #2 – as a Docker container


Option #3 – as a WinGet package


Managing versions of the application


Semantic versioning primer


Managing versions of a .NET tool


Managing versions of a Docker container


Managing versions of a WinGet package


Summary


Your turn!


Task #1 – allowing Linux users to install Bookmarkr using apt-get


Task #2 – allowing macOS users to install Bookmarkr using Homebrew


Part 5: Advanced Techniques and Best Practices


12


Performance Optimization and Tuning


Technical requirements


Performance optimization areas


Instrumenting .NET applications


Hot spots versus hot paths


Identifying the application’s hot spots and hot paths


Profiling Bookmarkr with BenchmarkDotNet


Monitoring BookmarkrSyncr with Azure Application Insights


Common performance optimization techniques


Optimizing Bookmarkr’s performance


Summary


Your turn!


Task #1 – Write more...



Belmansour Tidjani :

Tidjani Belmansour is an expert in developing and architecting solutions on the Microsoft Azure platform, particularly in.NET, with over 21 years of experience. His passion for development began early—he wrote his first program in QuickBasic at the age of eight and has never stopped since. He is currently the Director of the Azure Centre of Excellence at Cofomo, where he collaborates with organizations of all sizes, across both public and private sectors, and on projects around the globe. Since 2019, Tidjani has been recognized as a Microsoft Azure MVP. He is also the co-host of the Azure Quebec Community, a trainer, a blogger, and an international speaker. Tidjani holds a BSc in Computer Science and a Ph.D. in Engineering.



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.