Skip to content

Instantly share code, notes, and snippets.

@prmichaelsen
Created July 14, 2025 16:51
Show Gist options
  • Save prmichaelsen/c34ab0a189bea0d2eb32ff9127467020 to your computer and use it in GitHub Desktop.
Save prmichaelsen/c34ab0a189bea0d2eb32ff9127467020 to your computer and use it in GitHub Desktop.
Demonstrates building rich completion via yargs
import yargs, { Arguments } from 'yargs';
import fs from 'fs';
export const cli = yargs(process.argv)
.scriptName('confluence-sync')
.usage('$0 <command> [options]')
.demandCommand(1, 'You need to specify a command')
.completion('completion', (current: string, argv: Arguments<any>, completionFilter, done) => {
const [_, command] = argv._;
/**
* First check to see if the user is actively typing
* a valid command. commands for this
* program are 'sync' and 'download'.
*/
const completions = ['sync', 'download']
.filter(c => c.startsWith(command));
switch (command) {
case "sync":
{
/** If --destination is provided, then
* we can provide the file path completion
*/
if ("destination" in argv || "d" in argv) {
completionFilter(() => {
const sourcePath = process.cwd();
if (fs.existsSync(sourcePath)) {
const stats = fs.statSync(sourcePath);
if (stats.isDirectory()) {
const keys = fs.readdirSync(sourcePath);
done(keys);
}
}
});
}
break;
}
case "download":
{
/** If --source is provided, then
* we can provide the file path completion
*/
if ("source" in argv || "s" in argv) {
completionFilter(() => {
const sourcePath = process.cwd();
if (fs.existsSync(sourcePath)) {
const stats = fs.statSync(sourcePath);
if (stats.isDirectory()) {
const keys = fs.readdirSync(sourcePath);
done(keys);
}
}
});
}
break;
}
default: {
completionFilter(() => done(completions));
}
}
})
.strict()
.help()
.alias('h', 'help')
.version()
.alias('v', 'version');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment