Skip to content

Commit

Permalink
Merge pull request #5 from Illu/feat/parser
Browse files Browse the repository at this point in the history
add parser to better handle args
  • Loading branch information
fbarrailla authored Sep 13, 2018
2 parents bfd9113 + 26d3256 commit cd1929f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ $ pypy 10 💩
💩
```

Pypy will automatically copy the pyramid in your clipboard, unless the `--no-copy` option is specified.
Pypy will automatically copy the pyramid to your clipboard, unless the `--no-copy` option is specified.

## Options

### `--no-space`
### `-s` `--no-space`

No space between items

### `--no-copy`
### `-c` `--no-copy`

Don't copy the pyramid in your clipboard
Don't copy the pyramid to the clipboard

## Licence ##
WTFPL License
Expand Down
57 changes: 39 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
#!/usr/bin/env node

const cp = require("copy-paste");
const ArgumentParser = require("argparse").ArgumentParser;
const {version} = require('./package.json');

const params = process.argv;
params.splice(0, 2);
if (!params || params.length < 2) {
console.log('Usage: pypy 3 😎');
return;
}

const noSpace = params.includes('--no-space');
const noCopy = params.includes('--no-copy');

const generatePyramid = (size, emoji) => {
if (size < 2) {
console.log("Pyramid can't be smaller than 2 emojis");
return;
const parser = new ArgumentParser({
version,
addHelp: true,
description: 'Generate pyramids'
});
parser.addArgument(
'size',
{
type: 'int',
help: 'Size of the pyramid',
})
parser.addArgument(
'emoji',
{
help: 'emoji/text to be displayed inside the pyramid',
})
parser.addArgument(
[ '-s', '--no-space' ],
{
default: false,
nargs: 0,
help: 'Remove space between emojis',
dest: 'noSpace',
}
if (isNaN(Number(size))){
console.log("Size must be a number");
return;
);
parser.addArgument(
[ '-c', '--no-copy' ],
{
default: false,
nargs: 0,
help: "Disable copying the pyramid to the clipboard",
dest: 'noCopy'
}
);

const args = parser.parseArgs();

const generatePyramid = ({size, emoji, noSpace, noCopy}) => {
const result = [];
for (let i = 1; i < size * 2; i++) {
let n = i > size ? size * 2 - i : i;
Expand All @@ -32,4 +53,4 @@ const generatePyramid = (size, emoji) => {
}
};

generatePyramid(...params);
generatePyramid(args);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"pypy": "./index.js"
},
"dependencies": {
"argparse": "^1.0.10",
"copy-paste": "^1.3.0"
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# yarn lockfile v1


argparse@^1.0.10:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
dependencies:
sprintf-js "~1.0.2"

copy-paste@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/copy-paste/-/copy-paste-1.3.0.tgz#a7e6c4a1c28fdedf2b081e72b97df2ef95f471ed"
Expand All @@ -20,6 +26,10 @@ iconv-lite@^0.4.8:
version "2.1.2"
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"

sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"

sync-exec@~0.6.x:
version "0.6.2"
resolved "https://registry.yarnpkg.com/sync-exec/-/sync-exec-0.6.2.tgz#717d22cc53f0ce1def5594362f3a89a2ebb91105"

0 comments on commit cd1929f

Please sign in to comment.