CodeSampleX

샘플

commander 15.0.0: Prevent silent boolean option default regression in Commander 15 by accounting for paired positive/negative flags remaining undefined instead of defaulting to true like lone negated flags

검증된 샘플 — npm commander 15.0.0: Prevent silent boolean option default regression in Commander 15 by accounting for paired positive/negative flags remaining…

sha256:e9734b35dd9bf261570327aa6767cba61b29225dbb1b6eebc59d47cb344f1141

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
3
빌드한 서명 키
3
선언된 환경 node linux x64 node node npm

검증 실행 환경

환경 컨트랙트 단계 실행일
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-16
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-18
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-09-08

케이스

HOW
목표
Prevent silent boolean option default regression in Commander 15 by accounting for paired positive/negative flags remaining undefined instead of defaulting to true like lone negated flags
패키지
심벌
  • Command
  • Option
  • Command.option
  • Command.opts
  • Command.getOptionValueSource
  • Command.parse
  • Command.addOption
  • Option.default
환경
node
생성일
2026-08-16T07:26:06Z

컨트랙트

  1. assert lone negated option (--no-cache) defaults opts().cache to true with source 'default' when omitted from CLI
  2. assert paired positive and negative options (--cache + --no-cache) leave opts().cache undefined without implicit true default in Commander 15
  3. assert negative-first paired options (--no-cache + --cache) also leave opts().cache undefined when omitted
  4. assert explicit CLI flags on dual options resolve accurately to true for --cache and false for --no-cache with source 'cli'
  5. assert explicit Option.default(true) is required on paired options to restore default true behavior in Commander 15
  6. assert action handler callback receives undefined for omitted dual options

파일

  • NOTES.md
  • csx.json
  • package-lock.json
  • package.json
  • test/contract.mjs

소스 아티팩트 내려받기 (tar.gz)

소스

NOTES.md
# Notes on Commander 15 Option Defaults

## search_known_solution Result
`search_known_solution` answered with fallback matches for unrelated libraries (`detect-libc` platform probing and `tokio::watch` channel acknowledgement) with no existing verified sample for `commander`.

## What a Model Would Have Written Instead
A model would register paired flags (`program.option('--cache').option('--no-cache')`) and write `if (program.opts().cache) ...` assuming that defining `--no-cache` implies `cache: true` by default when no flags are supplied on CLI.

## How the Wrong Version Fails
Fails silently with a green build: in Commander <=14 defining both `--cache` and `--no-cache` automatically populated `{ cache: true }` in `opts()`, but in Commander 15 paired positive and negative options leave `cache` as `undefined` (tri-state), silently causing boolean feature flags to evaluate as falsy/undefined without raising any CLI error.
csx.json
{"case":{"caseId":"case:sha256:01a7ed0da9c032b9110e7b758f44302e102ecd2a144150c4124cf1b95d206998","contract":["assert lone negated option (--no-cache) defaults opts().cache to true with source 'default' when omitted from CLI","assert paired positive and negative options (--cache + --no-cache) leave opts().cache undefined without implicit true default in Commander 15","assert negative-first paired options (--no-cache + --cache) also leave opts().cache undefined when omitted","assert explicit CLI flags on dual options resolve accurately to true for --cache and false for --no-cache with source 'cli'","assert explicit Option.default(true) is required on paired options to restore default true behavior in Commander 15","assert action handler callback receives undefined for omitted dual options"],"goal":"Prevent silent boolean option default regression in Commander 15 by accounting for paired positive/negative flags remaining undefined instead of defaulting to true like lone negated flags","kind":"HOW","packages":["pkg:npm/commander@15.0.0"],"schemaVersion":1,"symbols":["Command","Option","Command.option","Command.opts","Command.getOptionValueSource","Command.parse","Command.addOption","Option.default"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/commander@15.0.0"],"schemaVersion":1,"symbols":["Command","Option","Command.option","Command.opts","Command.getOptionValueSource","Command.parse","Command.addOption","Option.default"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "npm-commander-version",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "npm-commander-version",
      "version": "1.0.0",
      "license": "ISC",
      "dependencies": {
        "commander": "^15.0.0"
      }
    },
    "node_modules/commander": {
      "version": "15.0.0",
      "resolved": "https://registry.npmjs.org/commander/-/commander-15.0.0.tgz",
      "integrity": "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg==",
      "license": "MIT",
      "engines": {
        "node": ">=22.12.0"
      }
    }
  }
}
package.json
{
  "name": "npm-commander-version",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "commander": "15.0.0"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import { Command, Option, program } from 'commander';

// ============================================================================
// 1. Lone Negated Option Defaults to True
// ============================================================================
// In Commander (all versions including v15), defining ONLY a negated option
// (--no-foo) treats the flag as an opt-out boolean and implicitly defaults
// the attribute value to `true` with source 'default' when no CLI flag is passed.
{
  const cmd = new Command();
  cmd.option('--no-cache', 'disable caching');
  cmd.parse([], { from: 'user' });

  const opts = cmd.opts();
  assert.equal(
    opts.cache,
    true,
    'Lone negated option (--no-cache) must implicitly default to true when no args provided',
  );
  assert.equal(
    cmd.getOptionValueSource('cache'),
    'default',
    "Lone negated option source must be 'default'",
  );
}

// ============================================================================
// 2. Dual Options (Positive + Negative) No Longer Default to True in v15
// ============================================================================
// Naive expectation / Pre-v15 behavior:
// Defining both `--cache` and `--no-cache` used to implicitly set `cache: true`
// because the `--no-cache` declaration triggered the boolean default.
// Commander 15 breaking change (#2405):
// When both positive and negative options are registered, Commander no longer
// sets an implicit default. The option remains `undefined` (supporting tri-state
// boolean logic: true if --cache, false if --no-cache, undefined if omitted).
{
  const cmd = new Command();
  cmd.option('--cache', 'enable caching')
     .option('--no-cache', 'disable caching');
  cmd.parse([], { from: 'user' });

  const opts = cmd.opts();
  assert.equal(
    opts.cache,
    undefined,
    'Dual options (--cache + --no-cache) must remain undefined when omitted in v15, not true',
  );
  assert.equal(
    cmd.getOptionValueSource('cache'),
    undefined,
    'Option value source must be undefined when dual options are omitted without explicit default',
  );
  assert.deepEqual(
    opts,
    {},
    'Options object must be empty when dual options are not passed on CLI',
  );
}

// ============================================================================
// 3. Declaration Order Independence for Dual Options
// ============================================================================
// In Commander 15, defining negative before positive (--no-cache then --cache)
// behaves identically: no implicit default is injected.
{
  const cmd = new Command();
  cmd.option('--no-cache', 'disable caching')
     .option('--cache', 'enable caching');
  cmd.parse([], { from: 'user' });

  const opts = cmd.opts();
  assert.equal(
    opts.cache,
    undefined,
    'Negative-first dual options (--no-cache + --cache) must also remain undefined when omitted',
  );
  assert.equal(cmd.getOptionValueSource('cache'), undefined);
}

// ============================================================================
// 4. Dual Options Explicit CLI Argument Resolution
// ============================================================================
// When explicitly provided on the command line, dual options resolve to
// true or false with source 'cli'.
{
  // Test --cache
  const cmdPos = new Command();
  cmdPos.option('--cache', 'enable caching')
        .option('--no-cache', 'disable caching');
  cmdPos.parse(['--cache'], { from: 'user' });
  assert.equal(cmdPos.opts().cache, true);
  assert.equal(cmdPos.getOptionValueSource('cache'), 'cli');

  // Test --no-cache
  const cmdNeg = new Command();
  cmdNeg.option('--cache', 'enable caching')
        .option('--no-cache', 'disable caching');
  cmdNeg.parse(['--no-cache'], { from: 'user' });
  assert.equal(cmdNeg.opts().cache, false);
  assert.equal(cmdNeg.getOptionValueSource('cache'), 'cli');
}

// ============================================================================
// 5. Explicit Defaulting on Dual Options
// ============================================================================
// If default `true` or `false` is desired with dual options in v15, it must be
// declared explicitly using Option.default() or passing a default argument.
{
  const cmd = new Command();
  cmd.addOption(new Option('--cache', 'enable caching').default(true))
     .addOption(new Option('--no-cache', 'disable caching'));
  cmd.parse([], { from: 'user' });

  assert.equal(
    cmd.opts().cache,
    true,
    'Explicit default on positive Option must be respected when dual options are omitted',
  );
  assert.equal(cmd.getOptionValueSource('cache'), 'default');

  // Overriding explicit default with --no-cache CLI flag
  const cmdOverride = new Command();
  cmdOverride.addOption(new Option('--cache', 'enable caching').default(true))
             .addOption(new Option('--no-cache', 'disable caching'));
  cmdOverride.parse(['--no-cache'], { from: 'user' });

  assert.equal(
    cmdOverride.opts().cache,
    false,
    'CLI flag --no-cache must override explicit default true',
  );
  assert.equal(cmdOverride.getOptionValueSource('cache'), 'cli');
}

// ============================================================================
// 6. Action Handler Integration
// ============================================================================
// Action handler receives the exact tri-state option values.
{
  let receivedOptions = null;
  const cmd = new Command();
  cmd.name('testapp')
     .option('--verbose', 'enable verbose logging')
     .option('--no-verbose', 'disable verbose logging')
     .action((opts) => {
       receivedOptions = opts;
     });

  cmd.parse([], { from: 'user' });
  assert.notEqual(receivedOptions, null);
  assert.equal(
    receivedOptions.verbose,
    undefined,
    'Action handler must receive undefined for omitted dual options in v15',
  );
}

console.log('All Commander v15 contract assertions passed successfully.');

오리진 시더

csx-seed