Skip to content

Commit

Permalink
fixed color-less alignment, more tests, fixed empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed Jul 18, 2021
1 parent 7ce21ca commit 7743f5e
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "shell",
"command": "yarn build && npm publish build/",
"problemMatcher": [
"$tsc-watch"
"$tsc"
],
"group": "build",
"label": "deploy"
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"name": "massarg",
"version": "0.1.0-pre.11",
"version": "0.1.0-pre.12",
"description": "Flexible, powerful, and simple command/argument parser for CLI applications",
"main": "index.js",
"repository": "https://github.com/chenasraf/massarg.git",
"author": "Chen Asraf <chenasrafil@gmail.com>",
"license": "Apache",
"scripts": {
"clean": "rm -rf build",
"build": "yarn clean && tsc -p tsconfig.build.json && cp package.json README.md src/*.d.ts build",
"build": "echo $(pwd); yarn clean && tsc -p tsconfig.build.json && cp package.json README.md src/*.d.ts build",
"develop": "tsc --watch",
"prepack": "yarn build",
"test": "jest"
},
"devDependencies": {
Expand Down
21 changes: 11 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,18 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
].join(" ")
)

lines.push("")

if (this._help.header) {
lines.push("")
lines.push(this.color(bodyColors, this._help.header))
lines.push("")
}

if (this._commands.length) {
lines.push("")
lines.push(this.color(titleColors, "Commands:"))
lines.push("")
lines.push(...this._printCommands())
lines.push("")
}

lines.push(...this._printOptions())
Expand Down Expand Up @@ -308,19 +310,18 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {

const ARG_SPACE_LEN = 2
const INDENT_LEN = 2
const COLOR_COUNT = 2
const nameFullSize = maxNameLen + ARG_SPACE_LEN + INDENT_LEN

for (const item of list) {
const cmdName = this.color(highlightColors, `${item.name}`).padEnd(
nameFullSize + (COLOR_COUNT - 1) * COLOR_CODE_LEN,
nameFullSize + (this._help.useColors ? colorCount(highlightColors) : 0) * COLOR_CODE_LEN,
" "
)
const cmdDescr = this.color(normalColors, item.description ?? "")

for (const line of wrap(cmdName + cmdDescr, {
indent: nameFullSize + INDENT_LEN,
colorCount: colorCount(normalColors, highlightColors),
colorCount: this._help.useColors ? colorCount(normalColors, highlightColors) : 0,
firstLineIndent: INDENT_LEN,
printWidth: this._help.printWidth,
})) {
Expand All @@ -338,7 +339,7 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
for (const line of this._getWrappedLines(
this._commands.map((c) => ({ name: this._fullCmdName(c), description: c.description }))
)) {
if (line.length === 0 || line.trim().length) {
if (line.trim().length) {
lines.push(line)
}
}
Expand All @@ -352,27 +353,26 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {

const commandOpts: string[] = []

commandOpts.push("")

for (const cmd of this._commands) {
const opts = this._commandOptions(cmd)
if (opts.length) {
commandOpts.push(this.color(subtitleColors, `${cmd.name}:`))
commandOpts.push("")
for (const line of this._getWrappedLines(
opts.map((c) => ({ name: this._fullOptName(c), description: c.description }))
)) {
if (line.length === 0 || line.trim().length) {
if (line.trim().length) {
commandOpts.push(line)
}
}
}
}

lines.push(this.color(titleColors, commandOpts.length ? "Command Options:" : "Options:"))
lines.push("")

for (const line of commandOpts) {
lines.push(line)
lines.push("")
}

const globalOpts = this._globalOptions()
Expand All @@ -386,6 +386,7 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
)) {
if (line.trim().length) {
lines.push(line)
lines.push("")
}
}
}
Expand Down
82 changes: 47 additions & 35 deletions src/sample.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,56 @@
import massarg from "."

massarg()
.help({
// printWidth: 0,
binName: "my-cmd",
useGlobalColumns: true,
header: "This is the app description",
footer: "Copyright",
usageExample: "command [options]",
})
.option({
name: "bool",
aliases: ["b"],
defaultValue: false,
commands: ["do", "cc"],
description: "This is a boolean arg. Supply it without value to set as true, or set value 0 for false",
required: true,
parse: Boolean,
// .help({
// // printWidth: 0,
// binName: "my-cmd",
// useGlobalColumns: true,
// header: "This is the app description",
// footer: "Copyright",
// usageExample: "command [options]",
// useColors: false,
// })
// .option({
// name: "bool",
// aliases: ["b"],
// defaultValue: false,
// commands: ["do", "cc"],
// description: "This is a boolean arg. Supply it without value to set as true, or set value 0 for false",
// required: true,
// parse: Boolean,
// })
// .option({
// name: "number",
// aliases: ["n"],
// description: "This is a number arg, if you include this option, you must supply it with a value.",
// defaultValue: 0,
// commands: "do",
// parse: (v) => parseInt(v),
// })
// .command({
// name: "do-something",
// description: "This command does something. This description is just to fill the blanks. Don't kill the messenger.",
// aliases: ["do", "d"],
// run: console.log.bind(undefined, "do"),
// })
// .command({
// name: "my-custom-command",
// description:
// "This is another command that does something. It's a different one just to see more available. This " +
// "description is just to fill the blanks. Don't kill the messenger.",
// aliases: ["cc", "c"],
// run: console.log.bind(undefined, "do"),
// })
// .main(console.log.bind(undefined, "main"))
.command({
name: "cmd",
description: "Command",
run: () => void 0,
})
.option({
name: "number",
aliases: ["n"],
description: "This is a number arg, if you include this option, you must supply it with a value.",
defaultValue: 0,
commands: "do",
description: "Number value",
commands: "cmd",
parse: (v) => parseInt(v),
})
.command({
name: "do-something",
description: "This command does something. This description is just to fill the blanks. Don't kill the messenger.",
aliases: ["do", "d"],
run: console.log.bind(undefined, "do"),
})
.command({
name: "my-custom-command",
description:
"This is another command that does something. It's a different one just to see more available. This " +
"description is just to fill the blanks. Don't kill the messenger.",
aliases: ["cc", "c"],
run: console.log.bind(undefined, "do"),
})
.main(console.log.bind(undefined, "main"))
.parse()
94 changes: 94 additions & 0 deletions tests/help.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import massarg from "../src"

describe("print help", () => {
test("should print help without command options", () => {
const helpStr = massarg()
.help({ useColors: false })
.option({
name: "number",
description: "Number value",
parse: (v) => parseInt(v),
})
.getHelpString()
.join("\n")

expect(helpStr).toBe(
"Usage: processChild.js [command] [options]" +
"\n\n" +
"Options:" +
"\n\n" +
" --help|-h Display help information" +
"\n\n" +
" --number Number value" +
"\n"
)
})
test("should print help correctly with only global options", () => {
const helpStr = massarg()
.help({ useColors: false })
.command({
name: "cmd",
description: "Command",
run: () => void 0,
})
.option({
name: "number",
description: "Number value",
parse: (v) => parseInt(v),
})
.getHelpString()
.join("\n")

expect(helpStr).toBe(
"Usage: processChild.js [command] [options]" +
"\n\n" +
"Commands:" +
"\n\n" +
" cmd Command" +
"\n\n" +
"Options:" +
"\n\n" +
" --help|-h Display help information" +
"\n\n" +
" --number Number value" +
"\n"
)
})

test("should print help correctly with command options", () => {
const helpStr = massarg()
.help({ useColors: false })
.command({
name: "cmd",
description: "Command",
run: () => void 0,
})
.option({
name: "number",
description: "Number value",
commands: "cmd",
parse: (v) => parseInt(v),
})
.getHelpString()
.join("\n")

expect(helpStr).toBe(
"Usage: processChild.js [command] [options]" +
"\n\n" +
"Commands:" +
"\n\n" +
" cmd Command" +
"\n\n" +
"Command Options:" +
"\n\n" +
"cmd:" +
"\n\n" +
" --number Number value" +
"\n\n" +
"Global Options:" +
"\n\n" +
" --help|-h Display help information" +
"\n"
)
})
})
22 changes: 0 additions & 22 deletions tests/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,4 @@ describe("Options", () => {
const arr2el = massarg().option(opts).parse(["--array", "something", "--array", "another"])
expect(arr2el).toHaveProperty("array", ["something", "another"])
})

test("should print help properly", () => {
const helpStr = massarg()
.help({ useColors: false })
.option({
name: "number",
description: "Number value",
parse: (v) => parseInt(v),
})
.getHelpString()
.join("\n")

expect(helpStr).toBe(
"Usage: processChild.js [command] [options]" +
"\n\n" +
"Options:" +
"\n" +
" --help|-h Display help information" +
"\n" +
" --number Number value"
)
})
})

0 comments on commit 7743f5e

Please sign in to comment.