Skip to content

Commit f9c72a7

Browse files
authored
docs: update examples to run from examples folder (#2463)
1 parent e02c91b commit f9c72a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+458
-460
lines changed

docs/examples.md

Lines changed: 125 additions & 143 deletions
Large diffs are not rendered by default.

example/bool.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
const argv = require('yargs/yargs')(process.argv.slice(2)).parse();
3+
4+
if (argv.s) {
5+
console.log(argv.fr ? 'Le chat dit: ' : 'The cat says: ');
6+
}
7+
console.log((argv.fr ? 'miaou' : 'meow') + (argv.p ? '.' : ''));

example/bool.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

example/boolean_double.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
const argv = require('yargs/yargs')(process.argv.slice(2))
3+
.boolean(['x', 'y', 'z'])
4+
.parse();
5+
console.dir([argv.x, argv.y, argv.z]);
6+
console.dir(argv._);

example/boolean_double.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

example/boolean_single.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
const argv = require('yargs/yargs')(process.argv.slice(2))
3+
.boolean(['r', 'v'])
4+
.parse();
5+
console.dir([argv.r, argv.v]);
6+
console.dir(argv._);

example/boolean_single.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

example/cmds/init.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

example/cmds/init.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const command = 'init [dir]';
2+
export const desc = 'Create an empty repo';
3+
export const builder = {
4+
dir: {
5+
default: '.',
6+
},
7+
};
8+
export const handler = function (argv) {
9+
console.log('init called for dir', argv.dir);
10+
};

example/cmds/remote.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

example/cmds/remote.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const command = 'remote <command>';
2+
export const description = 'Manage set of tracked repos';
3+
export const builder = function (yargs) {
4+
return yargs.commandDir('remote_cmds');
5+
};
6+
export const handler = function (argv) {};

example/cmds/remote_cmds/add.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

example/cmds/remote_cmds/add.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const command = 'add <name> <url>';
2+
export const desc = 'Add remote named <name> for repo at url <url>';
3+
export const builder = {};
4+
export const handler = function (argv) {
5+
console.log('adding remote %s at url %s', argv.name, argv.url);
6+
};

example/cmds/remote_cmds/prune.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

example/cmds/remote_cmds/prune.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const command = 'prune <name> [names..]';
2+
export const desc = 'Delete tracked branches gone stale for remotes';
3+
export const builder = {};
4+
export const handler = function (argv) {
5+
console.log(
6+
'pruning remotes %s',
7+
[].concat(argv.name).concat(argv.names).join(', ')
8+
);
9+
};

example/command_hierarchy.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

example/command_hierarchy.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env node
2+
import yargs from 'yargs';
3+
4+
yargs(process.argv.slice(2)).commandDir('cmds').demandCommand().help().parse();

example/complex.js renamed to example/complex.mjs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
import yargs from 'yargs';
2+
13
// a fairly complex CLI defined using the yargs 3.0 API:
2-
var argv = require('yargs/yargs')(process.argv.slice(2))
4+
const argv = yargs(process.argv.slice(2))
35
.usage('Usage: $0 <cmd> [options]') // usage string of application.
46
.command('install', 'install a package (name@version)') // describe commands available.
5-
.command('publish', 'publish the package inside the current working directory')
6-
.option('f', { // document options.
7+
.command(
8+
'publish',
9+
'publish the package inside the current working directory'
10+
)
11+
.option('f', {
12+
// document options.
713
array: true, // even single values will be wrapped in [].
814
description: 'an array of files',
915
default: 'test.js',
10-
alias: 'file'
16+
alias: 'file',
1117
})
1218
.alias('f', 'fil')
1319
.option('h', {
1420
alias: 'help',
15-
description: 'display help message'
21+
description: 'display help message',
1622
})
1723
.string(['user', 'pass'])
1824
.implies('user', 'pass') // if 'user' is set 'pass' must be set.

example/count.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

example/count.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env node
2+
import yargs from 'yargs';
3+
4+
const argv = yargs(process.argv.slice(2))
5+
.count('verbose')
6+
.alias('v', 'verbose')
7+
.parse();
8+
9+
const VERBOSE_LEVEL = argv.verbose;
10+
11+
function WARN() {
12+
VERBOSE_LEVEL >= 0 && console.log.apply(console, arguments);
13+
}
14+
function INFO() {
15+
VERBOSE_LEVEL >= 1 && console.log.apply(console, arguments);
16+
}
17+
function DEBUG() {
18+
VERBOSE_LEVEL >= 2 && console.log.apply(console, arguments);
19+
}
20+
21+
WARN('Showing only important stuff');
22+
INFO('Showing semi-important stuff too');
23+
DEBUG('Extra chatty mode');

example/default_hash.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

example/default_hash.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
import yargs from 'yargs';
3+
4+
const argv = yargs(process.argv.slice(2)).default({x: 10, y: 10}).parse();
5+
console.log(argv.x + argv.y);

example/default_singles.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

example/default_singles.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
import yargs from 'yargs';
3+
4+
const argv = yargs(process.argv.slice(2))
5+
.default('x', 10)
6+
.default('y', 10)
7+
.parse();
8+
console.log(argv.x + argv.y);

example/demand_count.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

example/demand_count.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
import yargs from 'yargs';
3+
4+
const argv = yargs(process.argv.slice(2)).demand(2).parse();
5+
console.dir(argv);

example/divide.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

example/divide.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env node
2+
import yargs from 'yargs';
3+
4+
const argv = yargs(process.argv.slice(2))
5+
.usage('Usage: $0 -x [num] -y [num]')
6+
.demand(['x', 'y'])
7+
.parse();
8+
9+
console.log(argv.x / argv.y);

example/help.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

example/help.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env node
2+
import yargs from 'yargs';
3+
4+
const argv = yargs(process.argv.slice(2))
5+
.usage('This is my awesome program\n\nUsage: $0 [options]')
6+
.help('help')
7+
.alias('help', 'h')
8+
.version('version', '1.0.1')
9+
.alias('version', 'V')
10+
.options({
11+
input: {
12+
alias: 'i',
13+
description: '<filename> Input file name',
14+
requiresArg: true,
15+
required: true,
16+
},
17+
output: {
18+
alias: 'o',
19+
description: '<filename> output file name',
20+
requiresArg: true,
21+
required: true,
22+
},
23+
})
24+
.parse();
25+
26+
console.log('Inspecting options');
27+
console.dir(argv);
28+
29+
console.log('input:', argv.input);
30+
console.log('output:', argv.output);

example/implies.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

example/implies.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env node
2+
import yargs from 'yargs';
3+
4+
const argv = yargs(process.argv.slice(2))
5+
.usage('Usage: $0 -x [num] -y [num]')
6+
.implies('x', 'y')
7+
.parse();
8+
9+
if (argv.x) {
10+
console.log(argv.x / argv.y);
11+
}

example/implies_hash.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

example/implies_hash.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env node
2+
import yargs from 'yargs';
3+
4+
const argv = yargs(process.argv.slice(2))
5+
.usage('Usage: $0 -x [num] -y [num] -w [msg] -h [msg]')
6+
.implies({
7+
x: 'y',
8+
w: '--no-h',
9+
1: 'h',
10+
})
11+
.parse();
12+
13+
if (argv.x) {
14+
console.log('x / y : ' + argv.x / argv.y);
15+
}
16+
17+
if (argv.y) {
18+
console.log('y: ' + argv.y);
19+
}
20+
21+
if (argv.w) {
22+
console.log('w: ' + argv.w);
23+
}
24+
25+
if (argv.h) {
26+
console.log('h: ' + argv.h);
27+
}

0 commit comments

Comments
 (0)