CodeSampleX

Exemple

minimist 1.2.8: parse CLI arguments with string, boolean options, aliases and defaults using minimist

Échantillon vérifié pour npm minimist 1.2.8: parse CLI arguments with string, boolean options, aliases and defaults using minimist. Le contrat s'est exécuté…

sha256:6b1429ca854e1f6b53d3c78c0f4943b6814298d81c0ab3c6ad46e7d092d71e96

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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-28

Cas

HOW
Objectif
parse CLI arguments with string, boolean options, aliases and defaults using minimist
Paquets
Symboles
  • minimist
Créé
2026-08-28T05:34:00Z

Contrat

  1. minimist parses long and short boolean flags and supports negation with --no- prefix
  2. minimist parses string arguments passed with spaces or equal signs
  3. minimist forces specified option keys to string type and preserves leading zeros
  4. minimist forces specified option keys to boolean type without consuming following values
  5. minimist maps option aliases bidirectionally to full names and alias keys
  6. minimist applies default values for unspecified options without overriding provided values
  7. minimist collects remaining positional arguments in the _ array and arguments after -- in the -- array
  8. minimist supports stopEarly to treat all arguments after the first positional argument as positional

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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 CLI arguments with string, boolean options, aliases and defaults using minimist
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/minimist@1.2.8
Demonstrate these symbols/APIs:
  - minimist

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:225cee59f0e7bb95b6993989dce43dbfeeeea921f588433d2a6bdceca53c103a","contract":["minimist parses long and short boolean flags and supports negation with --no- prefix","minimist parses string arguments passed with spaces or equal signs","minimist forces specified option keys to string type and preserves leading zeros","minimist forces specified option keys to boolean type without consuming following values","minimist maps option aliases bidirectionally to full names and alias keys","minimist applies default values for unspecified options without overriding provided values","minimist collects remaining positional arguments in the _ array and arguments after -- in the -- array","minimist supports stopEarly to treat all arguments after the first positional argument as positional"],"goal":"parse CLI arguments with string, boolean options, aliases and defaults using minimist","kind":"HOW","packages":["pkg:npm/minimist@1.2.8"],"schemaVersion":1,"symbols":["minimist"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/minimist@1.2.8"],"schemaVersion":1,"subject":"pkg:npm/minimist@1.2.8","symbols":["minimist"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-minimist",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-minimist",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "minimist": "1.2.8"
      }
    },
    "node_modules/minimist": {
      "version": "1.2.8",
      "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
      "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-minimist",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "minimist": "1.2.8"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "parse CLI arguments with string, boolean options, aliases and defaults using minimist",
  "kind": "HOW",
  "packages": [
    "pkg:npm/minimist@1.2.8"
  ],
  "symbols": [
    "minimist"
  ]
}
src/index.mjs
import minimist from 'minimist';

/**
 * Parse command-line arguments using minimist with options.
 * @param {string[]} args Array of argument strings
 * @param {object} [opts] Parsing options (string, boolean, alias, default, etc.)
 * @returns {object} Parsed argument object
 */
export function parseArgs(args, opts) {
  return minimist(args, opts);
}

export { minimist };
export default minimist;
test/contract.mjs
import assert from 'node:assert/strict';
import { minimist, parseArgs } from '../src/index.mjs';

// 1. minimist parses long and short boolean flags and supports negation with --no- prefix
{
  const argv = minimist(['-v', '--verbose', '--no-debug', '-x']);
  assert.equal(argv.v, true);
  assert.equal(argv.verbose, true);
  assert.equal(argv.debug, false);
  assert.equal(argv.x, true);
}

// 2. minimist parses string arguments passed with spaces or equal signs
{
  const argv = parseArgs(['--host', 'example.com', '--port=8080', '-f', 'config.json']);
  assert.equal(argv.host, 'example.com');
  assert.equal(argv.port, 8080);
  assert.equal(argv.f, 'config.json');
}

// 3. minimist forces specified option keys to string type and preserves leading zeros
{
  const argv = minimist(['--code', '0123', '--id', '456'], {
    string: ['code', 'id']
  });
  assert.equal(argv.code, '0123');
  assert.equal(typeof argv.code, 'string');
  assert.equal(argv.id, '456');
  assert.equal(typeof argv.id, 'string');
}

// 4. minimist forces specified option keys to boolean type without consuming following values
{
  const argv = minimist(['-a', 'true', '-b', 'false', '-c', 'value'], {
    boolean: ['a', 'b', 'c']
  });
  assert.equal(argv.a, true);
  assert.equal(argv.b, false);
  assert.equal(argv.c, true);
  assert.deepEqual(argv._, ['value']);
}

// 5. minimist maps option aliases bidirectionally to full names and alias keys
{
  const argv = minimist(['-h', 'localhost', '-p', '3000'], {
    alias: {
      h: 'host',
      p: ['port', 'pnum']
    }
  });
  assert.equal(argv.h, 'localhost');
  assert.equal(argv.host, 'localhost');
  assert.equal(argv.p, 3000);
  assert.equal(argv.port, 3000);
  assert.equal(argv.pnum, 3000);
}

// 6. minimist applies default values for unspecified options without overriding provided values
{
  const argv = minimist(['--name', 'alice'], {
    default: {
      name: 'anonymous',
      timeout: 5000,
      enabled: true
    }
  });
  assert.equal(argv.name, 'alice');
  assert.equal(argv.timeout, 5000);
  assert.equal(argv.enabled, true);
}

// 7. minimist collects remaining positional arguments in the _ array and arguments after -- in the -- array
{
  const argv = minimist(['cmd', 'subcmd', '--opt', 'val', '--', 'extra1', '--flag', 'extra2'], {
    '--': true
  });
  assert.equal(argv.opt, 'val');
  assert.deepEqual(argv._, ['cmd', 'subcmd']);
  assert.deepEqual(argv['--'], ['extra1', '--flag', 'extra2']);
}

// 8. minimist supports stopEarly to treat all arguments after the first positional argument as positional
{
  const argv = minimist(['--bool', 'cmd', '--other', 'value'], {
    boolean: ['bool'],
    stopEarly: true
  });
  assert.equal(argv.bool, true);
  assert.deepEqual(argv._, ['cmd', '--other', 'value']);
  assert.equal(argv.other, undefined);
}

Seeder d'origine

anonyme