├── .gitignore ├── package.json ├── pnpm-lock.yaml ├── src └── index.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "practice-cli", 3 | "bin": { 4 | "practice": "./dist/index.js" 5 | }, 6 | "version": "0.0.0", 7 | "description": "", 8 | "main": "index.js", 9 | "scripts": { 10 | "link-cli": "yarn --global unlink practice-cli && chmod +x ./dist/index.js && yarn --global link", 11 | "dev": "tsc -w" 12 | }, 13 | "keywords": [], 14 | "author": "Matt Pocock", 15 | "license": "ISC", 16 | "devDependencies": { 17 | "@types/node": "^20.5.0", 18 | "typescript": "^5.1.6" 19 | }, 20 | "dependencies": { 21 | "commander": "^11.0.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /pnpm-lock.yaml: -------------------------------------------------------------------------------- 1 | lockfileVersion: 5.4 2 | 3 | specifiers: 4 | '@types/node': ^20.5.0 5 | commander: ^11.0.0 6 | typescript: ^5.1.6 7 | 8 | dependencies: 9 | commander: 11.0.0 10 | 11 | devDependencies: 12 | '@types/node': 20.5.0 13 | typescript: 5.1.6 14 | 15 | packages: 16 | 17 | /@types/node/20.5.0: 18 | resolution: {integrity: sha512-Mgq7eCtoTjT89FqNoTzzXg2XvCi5VMhRV6+I2aYanc6kQCBImeNaAYRs/DyoVqk1YEUJK5gN9VO7HRIdz4Wo3Q==} 19 | dev: true 20 | 21 | /commander/11.0.0: 22 | resolution: {integrity: sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ==} 23 | engines: {node: '>=16'} 24 | dev: false 25 | 26 | /typescript/5.1.6: 27 | resolution: {integrity: sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==} 28 | engines: {node: '>=14.17'} 29 | hasBin: true 30 | dev: true 31 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import { Command } from "commander"; 4 | 5 | // Declare the program 6 | 7 | const program = new Command(); 8 | 9 | // Add actions onto that CLI 10 | 11 | program 12 | .argument("", "string to log") 13 | .option("-c, --capitalize", "Capitalize the message") 14 | .action( 15 | ( 16 | message: string, 17 | opts: { 18 | capitalize?: boolean; 19 | }, 20 | ) => { 21 | if (opts.capitalize) { 22 | console.log(message.toUpperCase()); 23 | } else { 24 | console.log(message); 25 | } 26 | }, 27 | ) 28 | .description("Say hello"); 29 | 30 | program 31 | .command("add ") 32 | .action((numbers: number[]) => { 33 | const total = numbers.reduce((a, b) => a + b, 0); 34 | console.log(`Total: ${total}`); 35 | }) 36 | .description("Add numbers and log the total"); 37 | 38 | program 39 | .command("get-max-number ") 40 | .action((numbers: number[]) => { 41 | const max = Math.max(...numbers); 42 | console.log(`Max: ${max}`); 43 | }) 44 | .description("Add numbers and log the total"); 45 | 46 | // Execute the CLI with the given arguments 47 | 48 | program.parse(process.argv); 49 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "display": "Node LTS", 3 | "_version": "18.1.0", 4 | "compilerOptions": { 5 | "lib": [ 6 | "es2023" 7 | ], 8 | "outDir": "dist", 9 | "module": "Node16", 10 | "target": "es2022", 11 | "strict": true, 12 | "noUncheckedIndexedAccess": true, 13 | "esModuleInterop": true, 14 | "skipLibCheck": true, 15 | "forceConsistentCasingInFileNames": true, 16 | "moduleResolution": "node16" 17 | }, 18 | "include": [ 19 | "src" 20 | ] 21 | } --------------------------------------------------------------------------------