From 400d418763eb017116e28633a4d05cc905b9503a Mon Sep 17 00:00:00 2001 From: Slawomir Koszewski Date: Sun, 8 Feb 2026 15:10:06 +0100 Subject: [PATCH] Refactored usage writing helpers. --- src/cli.js | 89 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/src/cli.js b/src/cli.js index 0966449..63d9508 100755 --- a/src/cli.js +++ b/src/cli.js @@ -16,31 +16,90 @@ function usage() { return `Usage: sk-az-tools [options] Commands: - list-apps [--display-name|-n ] - list-app-permissions --app-id|-i [--resolve|-r] [--short|-s] [--filter|-f ] - list-app-grants --app-id|-i - list-resource-permissions [--app-id|-i | --display-name|-n ] - table [--header|-H ] + list-apps List Entra applications + list-app-permissions List required permissions for an app + list-app-grants List OAuth2 grants for an app + list-resource-permissions List available permissions for a resource app + table Render stdin JSON as Markdown table + +Global options (all commands): + -q, --query + -o, --output json|j|table|t|alignedtable|at|prettytable|pt + -h, --help + +Use: sk-az-tools --help +or: sk-az-tools --help`; +} + +function usageListApps() { + return `Usage: sk-az-tools list-apps [--display-name|-n ] [global options] Options: - -n, --display-name Filter apps by exact display name - -i, --app-id Application (client) ID + -n, --display-name Filter apps by exact display name`; +} + +function usageListAppPermissions() { + return `Usage: sk-az-tools list-app-permissions --app-id|-i [--resolve|-r] [--short|-s] [--filter|-f ] [global options] + +Options: + -i, --app-id Application (client) ID (required) -r, --resolve Resolve permission GUIDs to human-readable values -s, --short Makes output more compact - -f, --filter Filter by permission name glob - -q, --query Filter output JSON using JMESPath - -H, --header Header mode/spec: auto|a OR "col1, col2" OR "key1: Label 1, key2: Label 2" - -o, --output Output format: json|j|table|t|alignedtable|at|prettytable|pt (default: json) - -h, --help Show this help message`; + -f, --filter Filter by permission name glob`; +} + +function usageListAppGrants() { + return `Usage: sk-az-tools list-app-grants --app-id|-i [global options] + +Options: + -i, --app-id Application (client) ID (required)`; +} + +function usageListResourcePermissions() { + return `Usage: sk-az-tools list-resource-permissions [--app-id|-i | --display-name|-n ] [--filter|-f ] [global options] + +Options: + -i, --app-id Resource app ID + -n, --display-name Resource app display name + -f, --filter Filter by permission name glob`; +} + +function usageTable() { + return `Usage: sk-az-tools table [--header|-H ] [global options] + +Options: + -H, --header Header mode/spec: auto|a OR "col1, col2" OR "key1: Label 1, key2: Label 2"`; +} + +function usageCommand(command) { + switch (command) { + case "list-apps": + return usageListApps(); + case "list-app-permissions": + return usageListAppPermissions(); + case "list-app-grants": + return usageListAppGrants(); + case "list-resource-permissions": + return usageListResourcePermissions(); + case "table": + return usageTable(); + default: + return `Unknown command: ${command}\n\n${usage()}`; + } } async function main() { const argv = process.argv.slice(2); const command = argv[0]; - if (command === "-h" || command === "--help") { + if (!command) { console.log(usage()); process.exit(0); } + if (command === "-h" || command === "--help") { + const helpCommand = argv[1]; + console.log(helpCommand ? usageCommand(helpCommand) : usage()); + process.exit(0); + } const { values } = parseArgs({ args: argv.slice(1), @@ -59,8 +118,8 @@ async function main() { allowPositionals: false, }); - if (values.help || !command) { - console.log(usage()); + if (values.help) { + console.log(usageCommand(command)); process.exit(0); }