
Building CLI Applications with C# and .NET
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
- Implement advanced techniques for creating cross-platform, modular, and robust CLI applications
- Put your knowledge into practice through hands-on exercises and real-world projects
- Purchase of the print or Kindle book includes a free PDF eBook
Book DescriptionDevelopers 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.What you will learn - Master CLI application development principles to enhance productivity
- Build modular and extensible CLI applications that adapt to evolving needs
- Develop interactive CLI applications for engaging user experiences
- Integrate external APIs and services to extend functionality
- Implement robust security measures to ensure data protection
- Improve quality and reliability through comprehensive testing
- Package and deploy CLI applications efficiently for smooth releases
- Optimize performance to achieve high efficiency and effectiveness
Who this book is forThis book is for software developers, architects, and DevOps engineers aiming to enhance their existing SaaS platforms or optimize system operations. It focuses on providing users with CLI applications that automate and streamline workflows, unlocking operational efficiency. A basic understanding of programming concepts and prior experience with.NET and C# is expected, as this book doesn't cover introductory material.
All prices
More details
Persons
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.
Content
- Intro
- Title Page
- Copyright and Credits
- Dedication
- Foreword
- Contributors
- Learn more on Discord
- Table of Contents
- Preface
- Free Benefits with Your Book
- Part 1: Getting Started with CLI Applications
- Chapter 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
- Chapter 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!
- Chapter 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
- Chapter 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
- Chapter 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
- Chapter 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
- Chapter 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
- Chapter 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
- Chapter 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
- Chapter 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
- Chapter 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
- Chapter 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 benchmarks
- Task #2 - Fine-tune Bookmarkr for optimal performance
- Chapter 13: Security Considerations for CLI Applications
- Technical requirements
- Security areas
- Assessing the security posture of a CLI application
- Securing remote communications using authentication
- Why is authentication important?
- How to perform authentication
- Implementing authentication
- Authenticating external services using a PAT
- Passing the PAT from the CLI application to the external service
- Summary
- Your turn!
- Task #1 - Update dependency versions
- Task #2 - Use Mend Bolt to scan the code for vulnerabilities
- Task #3 - Allow BookmarkrSyncr to manage multiple users
- Chapter 14: Additional Resources and Libraries
- Further reading and resources
- C# 12 and .NET 8, by Mark J. Price
- Refactoring with C#, by Matt Eland
- Pragmatic Test-Driven Development in C# and .NET, by Adam Tibi
- C# 7 and .NET Core 2.0 High Performance, by Ovais Mehboob Ahmed Khan
- Useful libraries for CLI application development
- Polly
- HangFire
- StackExchange.Redis
- MediatR
- MassTransit
- BenchmarkDotNet
- Portable.BouncyCastle
- NSubstitute
- AutoFixture
- RichardSzalay.MockHttp
- Summary
- Your turn!
- Task #1 - List additional features for Bookmarkr
- Task #2 - List the skills and libraries you need to implement a feature
- Chapter 15: Unlock Your Exclusive Benefits
- Index
- About Packt
- Other Books You May Enjoy
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...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: ePUB
Copy protection: without DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use a reader that can handle the file format ePUB, such as Adobe Digital Editions or FBReader – both free (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePUB works well for novels and non-fiction books – i.e., 'flowing' text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook does not use copy protection or Digital Rights Management
For more information, see our eBook Help page.