Giter Site home page Giter Site logo

jcrqr / cmdr Goto Github PK

View Code? Open in Web Editor NEW
2.0 0.0 0.0 4 KB

A simple module to parse CLI arguments, flags and options for Deno.

Home Page: https://deno.land/x/cmdr

License: MIT License

TypeScript 100.00%
cli deno cli-parser command-line command-line-parser

cmdr's Introduction

cmdr

A simple module to parse CLI arguments, flags and options for Deno.

Goals

  • Simple to use
  • Easy configuration
  • Arguments validation (todo)
  • Auto-generated manual (todo)

NOTE: expect it to break before 1.0.0 release.

Getting Started

import { cmdr } from "https://deno.land/x/cmdr/mod.ts";

type Options = { name: string; help: boolean };

const { usage, match } cmdr<Options>("hello-world <NAME> -h, --help");

if (match.help) {
  console.info(usage.string);
  Deno.exit(0);
}

console.info(`Hello, ${match.name ? match.name : 'you there'}!`);

Example

Check more exaples here.

// main.ts

import { cmdr } from "https://deno.land/x/cmdr/mod.ts";

const { usage, match } = cmdr<Options>(
  "hello-world <NAME>... -q, --question=<QUESTION> -v, --version -h, --help",
)(Deno.args);

if (match.help) {
  console.info(usage.string);
  Deno.exit(0)
}

if (match.version) {
  console.log("Version: 0.0.0");
  Deno.exit(0);
}

if (match.name.length === 0) {
  console.error("Please, specify at least one <NAME>")
  Deno.exit(1);
}

const namesCombined = match.name.join(", ").replace(
  `, ${match.name[match.name.length - 1]}`,
  ` and ${match.name[match.name.length - 1]}`,
);

console.log(`Hello, ${namesCombined}!`);

if (match.question) {
  console.log(`Here's a question: ${match.question}`)
}

Running the example

$ deno run ./main.ts -q "what do you want for dinner?" John Alice Joe
# Hello, John, Alice and Joe!
# Here's a question: what do you want for dinner?

Compiling the example

$ deno compile --unstable --lite -o hello_world ./main.ts
# ...
$ ./hello_world -q "what do you want for dinner?" John Alice Joe
# Hello, John, Alice and Joe!
# Here's a question: what do you want for dinner?

Contributing

All contributions are very welcome!

If you find any bug or have a feature request, please open a new issue.

For code or documentation contributions, fork this repository, do your thing, and submit a Pull Request.

License

This project is released under the MIT License.

cmdr's People

Contributors

jcrqr avatar

Stargazers

Robert Laverty avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.