#!/usr/bin/env node

import meow from 'meow';
import getStdin from 'get-stdin';
import codenamer from './';

const minimistOptions = {
  string: 'word',
  alias: {
    w: 'word',
    c: 'count',
  },
};

const defaults = {
  word: ['cJ,n15', 'cN,a,n30'],
  count: 1,
};

const cli = meow(`
    Usage
      $ <input> | codenamer

    Defaults
      --word cJ,n15 --word cN,a,n30
        'codenames have two wirds where the first is an adjective with around 15
        letters, the next is a noun which alliterates and the total has around
        30 letters'
      --count 1
        return 1 codename

    Options
      -w, --word      specify based on a score system, e.g. 'pa' for a word with
                      prefix 'a', or 'l5' to allow only words with length 5. Can
                      be repeated, and scores can combined like 'pa,l5'.

    Score systems
      p<string>       Words starting with prefix, e.g. 'pa' for words starting
                      with a.
      n<integer>      Award combinations with this many letters, e.g. 'n10' to
                      increase probabilities of words with around 10 letters.
      c<letter>       Restrict this word to a word class:
                        - J for adjective
                        - N for noun
                        - R for adverb
                        - V for verb
      a               Alliterate (word rhyme) up to and including this word,
                      e.g. allow 'cheshire cat' but not 'cheshire dog'.

    Examples
      $ curl http://example.com | codenamer --word -pa --word -pb
`, minimistOptions);

getStdin().then(input => {
  if (!input) {
    /* eslint no-console:0 */
    console.error('ERR: No input given');
    console.log(cli.help);
    process.exit(0);
  }

  const options = Object.assign({}, defaults, cli.flags);
  const output = codenamer(options.word, input, options.count);

  console.log(output.map(code => code.join('-')).join('\n'));
});

@@ -139,20 +139,22 @@
 tring: '
-word
+format
 ',%0A  ali
@@ -167,16 +167,18 @@
     
-w: 'word
+f: 'format
 ',%0A 
@@ -226,26 +226,24 @@
 %7B%0A  
-word
+format
 : 
-%5B
 'cJ,n15
-', '
+-
 cN,a
@@ -251,9 +251,8 @@
 n30'
-%5D
 ,%0A  
@@ -331,274 +331,159 @@
     
-Defaults%0A      --word cJ,n15 --word cN,a,n30%0A        'codenames have two wirds where the first is an adjective with around 15%0A        letters, the next is a noun which alliterates and the total has around%0A        30 letters'%0A      --count 1%0A        return 1 codename
+Takes input from stdin and returns one or more codenames based on a format.%0A    The input may be HTML, in which case the main body of text is analyzed.
 %0A%0A  
@@ -503,19 +503,19 @@
    -
-w
+f
 , --
-word  
+format
     
@@ -522,16 +522,23 @@
 specify 
+format 
 based on
@@ -573,172 +573,376 @@
 or a
- word with%0A                      prefix 'a', or 'l5' to allow only words with length 5. Can%0A                      be repeated, and scores can combined like 'pa,l5'.
+%0A                      word with prefix 'a' (see below). Create multi-word%0A                      codenames using dashes (-) between specs, e.g. 'pa-pb' for%0A                      two words, the first starting with 'a', the next with 'b.'%0A                      Default: cJ,n15-cN,a,n30%0A      -c, --count     number of codenames to create%0A                      Default: 1
 %0A%0A  
@@ -978,16 +978,24 @@
 g%3E      
+ Prefix.
  Words s
@@ -1034,25 +1034,16 @@
 or words
- starting
 %0A       
@@ -1057,22 +1057,33 @@
         
+starting 
 with 
-a
+'a'
 .%0A      
@@ -1097,16 +1097,24 @@
 er%3E     
+ Normal.
  Award c
@@ -1157,17 +1157,8 @@
 e.g.
- 'n10' to
 %0A   
@@ -1176,16 +1176,25 @@
         
+'n10' to 
 increase
@@ -1231,24 +1231,76 @@
 round 10
- letters
+%0A                      letters. Adjust variance like 'n10/2'
 .%0A      
@@ -1314,16 +1314,28 @@
 r%3E      
+ Word class.
  Restric
@@ -1681,16 +1681,51 @@
 xamples%0A
+      $ cat myfile.txt %7C codenamer%0A
       $ 
@@ -1733,60 +1733,142 @@
 url 
+-s 
 http
+s
 ://e
-xample.com %7C codenamer --word -pa --word -pb
+n.wikipedia.org/wiki/Batman %7C codenamer%0A      $ curl -s https://en.wikipedia.org/wiki/Batman %7C codenamer --format pa-a,n15
 %0A%60, 
@@ -2147,20 +2147,33 @@
 options.
-word
+format.split('-')
 , input,

Use option `format` instead of repeated `words`