From 4f65227aff7d85163f1b9587cfe861c9196dcebe Mon Sep 17 00:00:00 2001 From: dperuo Date: Mon, 10 Jan 2022 22:24:29 -0600 Subject: [PATCH] feat: (*) Add '--count' flag for CLI --- README.md | 17 +++++++++++++++++ cli.ts | 5 ++++- src/const.ts | 6 ++++++ src/main.ts | 5 +---- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index df99c48..f25251e 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,23 @@ deno run https://deno.land/x/sweetid/cli.ts --xlong || -x # NsiHnUqK3cbADQ9cIzsi0Og0 ``` +## Generate Multiple IDs + +Use the `--count ` (`-c `) flag on the command line to generate +multiple IDs of the same length. + +```zsh +deno run https://deno.land/x/sweetid/cli.ts --count 3 || -c 3 +# hrWYHA +# GlTos0 +# YRH3Xe + +deno run https://deno.land/x/sweetid/cli.ts --medium --count 3 || -m -c 3 +# C71kkaC0BRSn +# CkBfX6pEjPDL +# siQRcoFa8fTz +``` + ## TypeScript Types Use [`SweetId`](src/const.ts) and [`SweetIdSize`](src/const.ts) to add type info diff --git a/cli.ts b/cli.ts index a5c8c2c..5ca10ae 100644 --- a/cli.ts +++ b/cli.ts @@ -6,5 +6,8 @@ import { getSizeString } from "./src/service.ts"; const args = parse(Deno.args, options); const size = getSizeString(args); +const { count } = args; -sweetid(size); +for (let i = count; i > 0; i--) { + console.log(sweetid(size)); +} diff --git a/src/const.ts b/src/const.ts index b69cefd..9c0c33b 100644 --- a/src/const.ts +++ b/src/const.ts @@ -21,6 +21,7 @@ export const enum CharSets { } export interface Sizes extends Args { + count: number; short: boolean; medium: boolean; long: boolean; @@ -29,9 +30,14 @@ export interface Sizes extends Args { export const options: ArgParsingOptions = { alias: { + count: "c", short: "s", medium: "m", long: "l", xlong: "x", }, + + default: { + count: 1, + }, }; diff --git a/src/main.ts b/src/main.ts index 93526dc..5f63c2d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,10 +4,7 @@ import { generate, getIdLength, isValid } from "./service.ts"; export function sweetid(size: SweetIdSize = "short"): SweetId { const id = generate(getIdLength(size)); - if (isValid(id)) { - console.log(id); - return id; - } + if (isValid(id)) return id; return sweetid(size); }