liveBook · Manning

Command-line argument

As explained in .NET in Action, Second Edition

A command-line argument is a value supplied to a program when it is invoked from a command-line interface.

Command-line arguments pass values to a program when it starts. In .NET, the `args` array holds them; spaces separate values unless quotes keep words together. Raw arrays become unwieldy when arguments need options, type conversion, or optional values. section 2.7

Common conventions make arguments easier to use:

  • Use a single dash for short options and a double dash for long options; aliases can offer both forms, such as `-a` and `--all`. section 2.7
  • Use a parser such as CommandLineParser to represent values and options, and to support required input, optional parameters, and help. section 2.7
  • Arguments can identify files to process, while standard input can supply additional filenames for piping. section 4.1.5
Listing 2.14 Options record to hold parsed command-line parameters
using CommandLine;                         #1
  
namespace HelloDotnet;                     #2
  
public record Options                      #3
{
  [Value(0, Required = true)]              #4
  public string? Text { get; init; }       #5
  
  [Option('f', "font")]                    #6
  public string? Font { get; init; }       #7
}

This options record maps a required positional text value and an optional `-f` or `--font` option to properties. section 2.7

How .NET in Action, Second Edition covers it

How deep it goes
Moves from argument conventions to parser-backed options and file-input handling, without detailing parser internals.
Examples
Uses ASCII-art text and font options, file searching, piped filenames, and short or long option forms.
What you can do after
Apply CommandLineParser to define required positional values and optional named options for a console application.
How it connects
Connects command-line parsing with standard input, piping, and regular expressions.

Also explained in

Compare how 14 books explain command-line argument →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.