Argu Documentation

repository·master·Indexed 19 days ago

https://github.com/fsprojects/argu

A declarative library for F# applications used to parse CLI arguments and XML configuration files and automatically generate help messages. It features a hierarchical parsing model for subcommands, flags, and parameters, and includes Argu.SourceGenerator to enable Ahead-of-Time (AOT) compilation and faster startup for large CLI templates.

Tokens
1.2K
Snippets
3
Records
7
Agent score
67%

What's inside Argu

  1. Current status of Argu.SourceGenerator

    master

    The Argu.SourceGenerator package is currently in an early stage. Only the marker attribute is available. The actual generation logic and the factory to consume the generated schema are planned for future releases.

    ComponentState
    [<ArguGenerate>] markerShipped
    Generator producing schemaPlanned
    Companion ArgumentParser factory consuming the schemaPlanned
  2. Use Argu.SourceGenerator for AOT compatibility

    master

    Argu.SourceGenerator is a companion package designed to enable Ahead-of-Time (AOT) compilation and faster startup for large CLI templates. It avoids the runtime reflection used by the core Argu package (such as FSharpType.GetUnionCases and Activator.CreateInstance), which is incompatible with publish-AOT because runtime-built generic types cannot be JIT-compiled.

    To opt-in to schema generation for a specific template, apply the [<ArguGenerate>] marker attribute to your Discriminated Union (DU).

    open Argu.SourceGenerator
    
    [<ArguGenerate>]
    type Args =
        | [<Mandatory>] Port of int
        | Verbose
        interface IArgParserTemplate with
            member this.Usage = "..."
  3. Install Argu via NuGet

    master

    Argu is a declarative CLI argument, XML configuration parser, and help message generator for F# applications. It is delivered as a netstandard2.0 NuGet package.

    # Install via NuGet package manager
    dotnet add package Argu
  4. How Argu parses command-line arguments

    master

    Argu uses a hierarchical parsing model to handle complex command-line interfaces, including subcommands, flags, and parameters.

    Core Concepts

    • Tokens: The raw input (e.g., -port, 8080) is read by a CliTokenReader.
    • Aggregators: As the parser traverses tokens, it uses CliParseResultAggregator to collect results. For nested subcommands, a stack of aggregators (CliParseResultAggregatorStack) is used to ensure results are routed to the correct command context.
    • Subcommands: When a subcommand is encountered, the parser creates a new aggregator context. Argu explicitly prohibits inheriting subcommands (a subcommand cannot be a child of another subcommand in a way that violates the hierarchy).
    • Parameter Types:
      • Primitives: Standard flags or values (e.g., --port 80).
      • Optional Parameters: May or may not be present.
      • List Parameters: Can collect multiple values (e.g., --files a.txt b.txt).
      • Subcommands: Triggers a nested parsing session for a new set of arguments.
  5. Handle command line errors and usage requests

    master

    Argu provides mechanisms to handle common CLI scenarios:

    Help and Usage

    If raiseOnUsage is set to true in the CliParseState, the parser will raise a HelpText exception when a help flag (defined in argInfo.HelpParam) is detected. If false, the IsUsageRequested property on the resulting parse results will be set to true.

    Unrecognized Arguments

    • If ignoreUnrecognized is true, tokens that do not match any defined parameter are added to the UnrecognizedCliParams list in the result.
    • If false, the parser will call error with ErrorCode.CommandLine, which typically terminates execution with a descriptive error message (e.g., unrecognized argument: '...').
  6. Parse command line arguments with `parseCommandLine`

    master

    To parse a command line, use the parseCommandLine function. This function initializes the parsing state, including the token reader, the result aggregator stack, and configuration for handling usage requests and unrecognized arguments.

    Parameters:

    • argInfo: The UnionArgInfo defining the expected command structure.
    • programName: The name of the executable.
    • description: An optional description of the program.
    • width: The character width used for formatting usage strings.
    • exiter: An implementation of IExiter to handle program exit.
    • raiseOnUsage: If true, encountering a help flag will raise a HelpText exception instead of just setting a flag.
    • ignoreUnrecognized: If true, unrecognized arguments are collected rather than causing an error.
    • inputs: The array of command-line strings to parse.
    // Note: This is a conceptual usage based on the internal API signature
    let results = parseCommandLine argInfo "my-app" (Some "A description") 80 exiter raiseOnUsage ignoreUnrecognized inputs