{"id":15042434,"url":"https://github.com/yjl9903/optc","last_synced_at":"2025-10-19T09:29:15.558Z","repository":{"id":37005862,"uuid":"484447578","full_name":"yjl9903/Optc","owner":"yjl9903","description":"An easy way to write TypeScript CLI Script","archived":false,"fork":false,"pushed_at":"2025-04-11T09:11:11.000Z","size":1162,"stargazers_count":27,"open_issues_count":10,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-11T11:38:04.066Z","etag":null,"topics":["bash","cli","javascript","python","script","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yjl9903.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-04-22T13:40:07.000Z","updated_at":"2025-03-16T17:02:21.000Z","dependencies_parsed_at":"2023-12-20T09:43:34.164Z","dependency_job_id":null,"html_url":"https://github.com/yjl9903/Optc","commit_stats":{"total_commits":365,"total_committers":3,"mean_commits":"121.66666666666667","dds":0.4712328767123287,"last_synced_commit":"c94d42e5a23799bd4c9c8d3ac3754aeabf244f4d"},"previous_names":[],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yjl9903%2FOptc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yjl9903%2FOptc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yjl9903%2FOptc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yjl9903%2FOptc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yjl9903","download_url":"https://codeload.github.com/yjl9903/Optc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248955547,"owners_count":21189158,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","cli","javascript","python","script","typescript"],"created_at":"2024-09-24T20:47:18.370Z","updated_at":"2025-10-19T09:29:10.494Z","avatar_url":"https://github.com/yjl9903.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Optc\n\n[![version](https://img.shields.io/npm/v/optc?color=rgb%2850%2C203%2C86%29\u0026label=Optc)](https://www.npmjs.com/package/optc) [![install size](https://packagephobia.com/badge?p=optc)](https://packagephobia.com/result?p=optc) [![CI](https://github.com/yjl9903/Optc/actions/workflows/ci.yml/badge.svg)](https://github.com/yjl9903/Optc/actions/workflows/ci.yml)\n\nAn easy way to write a single-file TypeScript command line application.\n\n## Installation\n\n```bash\nnpm i -g optc\n```\n\n## Usage\n\nSee example [echo.ts](./examples/echo.ts).\n\n```ts\n#!/usr/bin/env optc\n\nexport const name = 'echo';\n\nexport const version = '0.0.0';\n\nexport const description = 'Print some messages';\n\n// Echo some message\nexport default function echo(text: string) {\n  console.log(text);\n  return text;\n}\n\n// Greet someone\nexport function greet(name?: string, option?: { prefix: string }) {\n  const text = `${option?.prefix ?? 'Hello'}, ${name ?? 'Stranger'}`;\n  console.log(text);\n  return text;\n}\n```\n\nWhen running the above CLI script, Optc will automatically generate a default command with a required paramter `text`, and a subcommand `greet` with an optional paramter `name`.\n\n```bash\noptc examples/echo.ts word\n# word\n\noptc examples/echo.ts greet world\n# Hello, world\n\noptc examples/echo.ts --version\n# echo/0.0.0 win32-x64 node-v16.14.2\n\noptc examples/echo.ts --help\n# echo/0.0.0\n# \n# Usage: echo [OPTIONS] [COMMAND]\n# \n# Commands:\n#   echo \u003ctext\u003e        Echo some message\n#   echo greet [name]  Greet someone\n# \n# Options:\n#   -h, --help     Print help\n#   -v, --version  Print version\n\n# or use it directly, make sure you grant the executable permissions\n./examples/echo.ts greet Optc --prefix Hi\n# Hi, Optc\n```\n\nYou can see more examples in the [./examples](./examples).\n\n### Libraries\n\nOptc has some builtin functions based on some famous libs.\n\n+ `cd(dir: string)`: Change directory\n+ `pwd()`: Print working directory\n+ `` $`cmd` ``: Exec command like [zx](https://github.com/google/zx)\n+ `path`: [Node.js Path API](https://nodejs.org/api/path.html)\n+ `fs`: [fs-extra](https://www.npmjs.com/package/fs-extra)\n+ `glob`: [globby](https://www.npmjs.com/package/globby)\n+ `http`: [axios](https://www.npmjs.com/package/axios)\n\n### Custom Libraries\n\nYou can init a node module at `~/.optc/`, and create `~/.optc/dep.ts` to import all your custom libraries, functions and so on to your script execution environment.\n\n```ts\n// ~/.optc/dep.ts\n\n// Make sure that you have install \"kolorist\" in `~/.optc/`\nimport kolorist from 'kolorist'\n\nexport default function(global: any) {\n  global.color = kolorist\n}\n```\n\n### Limitation\n\n+ Optc extracts type infomation from the abstract syntax tree of the code (based on [babel](https://babeljs.io/)), so you **can not** do some type magic (union type, generic type and so on) on the types of paramters.\n\n+ Global code snippets can not have the type of global functions. Currently, it links to the local type declaration file.\n\n## Inspiration\n\n+ [argc](https://github.com/sigoden/argc): A handy way to handle sh/bash cli parameters.\n+ [zx](https://github.com/google/zx): A tool for writing better scripts\n+ [cac](https://github.com/cacjs/cac): Simple yet powerful framework for building command-line apps.\n\n## License\n\nMIT License © 2023 [XLor](https://github.com/yjl9903)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyjl9903%2Foptc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyjl9903%2Foptc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyjl9903%2Foptc/lists"}