CodeSampleX

샘플

cac 6.7.14: parse command line options with cac

검증된 샘플 — npm cac 6.7.14: parse command line options with cac. node 22 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다: cac creates a CLI instance with the…

sha256:c2bc680533f81a2a8d5c8555b1d790e74e291beb0d1f3461282f7757cdb020eb

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

실행 증거

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

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

검증 실행 환경

환경 컨트랙트 단계 실행일
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-08-26

케이스

HOW
목표
parse command line options with cac
패키지
심벌
  • cac
  • cac.command
  • cac.parse
환경
node 22.23.2
생성일
2026-08-26T09:16:48Z

컨트랙트

  1. cac creates a CLI instance with the specified name
  2. cac.command registers commands with positional arguments and options with default values
  3. cac.parse parses arguments for matched command and extracts positional args and option values
  4. cac.parse applies default option values when options are not provided

파일

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.js

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

소스

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: parse command line options with cac
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/cac@6.7.14
Demonstrate these symbols/APIs:
  - cac
  - cac.command
  - cac.parse

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:7f8693f0ec9174ab44d913606f33803ee8750cca3fd52f80c790b4f479239d5a","contract":["cac creates a CLI instance with the specified name","cac.command registers commands with positional arguments and options with default values","cac.parse parses arguments for matched command and extracts positional args and option values","cac.parse applies default option values when options are not provided"],"goal":"parse command line options with cac","kind":"HOW","packages":["pkg:npm/cac@6.7.14"],"schemaVersion":1,"symbols":["cac","cac.command","cac.parse"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/cac@6.7.14"],"schemaVersion":1,"subject":"pkg:npm/cac@6.7.14","symbols":["cac","cac.command","cac.parse"],"verifierAdapter":"node-typescript@1"}
index.js
const { cac } = require('cac');

/**
 * Creates and configures a CLI instance with commands and options.
 * @param {string} [name='cli-app'] - Name of the CLI application.
 * @returns {import('cac').CAC} Configured CAC instance.
 */
function createCli(name = 'cli-app') {
  const cli = cac(name);

  cli
    .command('build <entry>', 'Build the project from entry point')
    .option('--target <target>', 'Target output environment (e.g. node, browser)', {
      default: 'node',
    })
    .option('--minify', 'Minify the generated output bundle', {
      default: false,
    });

  cli
    .command('serve', 'Start local development server')
    .option('--port <port>', 'Port to listen on', {
      default: 3000,
    });

  return cli;
}

/**
 * Parses command line arguments using a CAC CLI instance.
 * @param {import('cac').CAC} cli - CAC instance.
 * @param {string[]} argv - Array of command line arguments (e.g. process.argv).
 * @param {object} [options] - CAC parse options (e.g. { run: false }).
 * @returns {import('cac').ParsedArgv} Parsed command line arguments and options.
 */
function parseArgs(cli, argv, options = { run: false }) {
  return cli.parse(argv, options);
}

module.exports = {
  createCli,
  parseArgs,
};
package-lock.json
{
  "name": "cac-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "cac-sample",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "cac": "6.7.14"
      }
    },
    "node_modules/cac": {
      "version": "6.7.14",
      "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz",
      "integrity": "sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ==",
      "license": "MIT",
      "engines": {
        "node": ">=8"
      }
    }
  }
}
package.json
{
  "name": "cac-sample",
  "version": "1.0.0",
  "description": "Parse command line options with cac",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "cac": "6.7.14"
  },
  "private": true,
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "parse command line options with cac",
  "kind": "HOW",
  "packages": [
    "pkg:npm/cac@6.7.14"
  ],
  "symbols": [
    "cac",
    "cac.command",
    "cac.parse"
  ]
}
test/contract.js
const assert = require('assert');
const { cac } = require('cac');
const { createCli, parseArgs } = require('../index.js');

// 1. Direct symbol test: cac factory
const cli = cac('test-cli');
assert.strictEqual(typeof cli, 'object', 'cac() should return a CLI instance object');
assert.strictEqual(cli.name, 'test-cli', 'cli.name should match the constructor argument');

// 2. Direct symbol test: cli.command and cli.parse
cli
  .command('bundle <entry>', 'Bundle project')
  .option('--format <format>', 'Output format', { default: 'esm' })
  .option('--sourcemap', 'Generate source maps');

const parsed = cli.parse(['node', 'cli.js', 'bundle', 'src/main.js', '--format', 'cjs', '--sourcemap'], { run: false });
assert.deepStrictEqual(
  parsed.args,
  ['src/main.js'],
  'cli.parse should extract positional command argument'
);
assert.strictEqual(
  parsed.options.format,
  'cjs',
  'cli.parse should parse explicit option value'
);
assert.strictEqual(
  parsed.options.sourcemap,
  true,
  'cli.parse should parse boolean flag option'
);

// 3. Test default options with helper
const helperCli = createCli('sample-tool');
const defaultParsed = parseArgs(helperCli, ['node', 'cli.js', 'build', 'index.ts'], { run: false });

assert.deepStrictEqual(
  defaultParsed.args,
  ['index.ts'],
  'helperCli should parse entry positional argument'
);
assert.strictEqual(
  defaultParsed.options.target,
  'node',
  'helperCli should apply default option value when omitted'
);
assert.strictEqual(
  defaultParsed.options.minify,
  false,
  'helperCli should apply default boolean false when omitted'
);

// 4. Test command with custom option values
const customParsed = parseArgs(
  helperCli,
  ['node', 'cli.js', 'build', 'index.ts', '--target', 'browser', '--minify'],
  { run: false }
);
assert.strictEqual(
  customParsed.options.target,
  'browser',
  'helperCli should override default option with user value'
);
assert.strictEqual(
  customParsed.options.minify,
  true,
  'helperCli should set boolean flag to true when passed'
);

console.log('Contract assertions passed.');

오리진 시더

익명