0Pricing
C# Academy · Lesson

CLI ergonomics

Build friendly command-line tools: clear help, short flags, exit codes, stdout vs stderr, basic parsing, and simple verbosity.

Friendly CLI basics

Goals:

  • Show help with -h/--help
  • Use clear flags (-i, -o, --verbose)
  • Return 0 on success, non-zero on error
  • Send results to stdout, diagnostics to stderr

Usage & help

Add a short usage section and accept both short and long flags; show it when input is missing.

using System;

// A tiny CLI that prints help if no args or -h/--help is passed.
public class Program
{
  static void PrintUsage()
  {
    Console.WriteLine("usage: tool -i <input> [-o <output>] [--verbose]");
    Console.WriteLine("options:");
    Console.WriteLine("  -i, --input     input file path (required)");
    Console.WriteLine("  -o, --output    output file path (optional)");
    Console.WriteLine("  -h, --help      show help");
    Console.WriteLine("      --verbose   print extra diagnostics to stderr");
  }

  public static void Main(string[] args)
  {
    if (args.Length == 0 || args[0] == "-h" || args[0] == "--help")
    {
      PrintUsage();
      Environment.ExitCode = 0;
      return;
    }

    Console.WriteLine("OK: arguments received"); // placeholder success
  }
}

All lessons in this course

  1. C# scripting (dotnet script), snippets & REPL
  2. Interop basics (P/Invoke) at a glance
  3. CLI ergonomics
← Back to C# Academy