Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:1mcp app Agent CLI Global Options Contract

From Leeroopedia
Knowledge Sources
Domains CLI, Configuration
Last Updated 2026-10-29 00:00 GMT

Overview

Pattern that centralizes cross-cutting CLI options—config paths, logging, telemetry, and session ownership—into a single shared contract so every command inherits the same flags and environment-variable mappings without re-declaring them.

Description

CLI Global Options Contract is the practice of defining common command-line options once and propagating them to every subcommand through a framework's middleware or inheritance mechanism. It addresses the problem of duplicated option declarations across a large CLI surface, which leads to inconsistent environment-variable mappings, divergent defaults, and maintenance burden. By registering options once with a guard against double-registration, all commands receive identical config/log/telemetry plumbing. Each option maps to an environment variable (following the `ONE_MCP_*` convention), so settings can be provided without command-line flags. Critical options that interact with process-level decisions—such as telemetry suppression—merge environment and command-line values so a flag can never weaken a launcher-enforced policy.

Usage

Use this principle when designing a CLI with multiple command groups that need to:

  • Share configuration file paths and directory settings across all commands
  • Provide consistent logging level and output file options
  • Gate telemetry collection with a single, env-var-backed contract
  • Ensure environment variables and command-line flags use one mapping layer

Theoretical Basis

The core mechanism combines two established patterns:

Shared Options Object defines options once as a declarative contract:

// Abstract pattern (NOT real implementation)
const sharedOptions = {
  config: { alias: 'c', type: 'string' },
  'log-level': { type: 'string', choices: ['debug','info','warn','error'] },
};

Registration Guard prevents duplicate parsing in nested builders:

// Abstract guard logic (NOT real implementation)
const registered = new WeakSet<Argv>();
function withShared(y: Argv): Argv {
  if (registered.has(y)) return y;
  registered.add(y);
  return y.options(sharedOptions);
}

The environment variable mapping follows the 12-Factor App principle: each option declares its env-var backing, so configuration can come from the environment or the command line interchangeably.

Practical Guide

  1. Declare all shared options in a single exported object with type-safe `as const` typing
  2. Use a WeakSet or equivalent guard so nested command builders register options exactly once
  3. Map every option to a `ONE_MCP_*` environment variable with matching semantics
  4. For security-sensitive options, merge environment and command-line values so neither can weaken the other
  5. Export a TypeScript interface so command handlers get typed access to global options

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment