Skip to content

Commit

Permalink
Renaming.
Browse files Browse the repository at this point in the history
Seems getopt was already taken, and also it was a horrible name.
  • Loading branch information
frodwith committed May 2, 2011
1 parent 3e83df6 commit 3d81dbd
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 103 deletions.
80 changes: 40 additions & 40 deletions README.markdown
@@ -1,22 +1,22 @@
Getopt YANOP - Yet Another Node Option Parser
====== ======


For the impatient For the impatient
----------------- -----------------
If you're in a hurry and you just want to parse your options already: If you're in a hurry and you just want to parse your options already:


getopt = require('getopt'); yanop = require('yanop');
options = getopt.simple({ options = yanop.simple({
verbose: { verbose: {
type: getopt.flag, type: yanop.flag,
short: 'v', short: 'v',
}, },
input: { input: {
type: getopt.list, type: yanop.list,
short: 'i', short: 'i',
}, },
output: { output: {
type: getopt.scalar, type: yanop.scalar,
description: 'output file (- for stdout)', description: 'output file (- for stdout)',
default: '-', default: '-',
short: 'o', short: 'o',
Expand Down Expand Up @@ -45,9 +45,9 @@ prints out:
-o VAL -o VAL
--output=VAL output file (- for stdout). Required. Default: - --output=VAL output file (- for stdout). Required. Default: -


getopt.simple() doesn't do what I want! yanop.simple() doesn't do what I want!
======================================= =======================================
Yeah, sorry about that. getopt.simple() does what I usually want with a Yeah, sorry about that. yanop.simple() does what I usually want with a
minimum of fuss and ceremony, but have a look at the lower level APIs. Very minimum of fuss and ceremony, but have a look at the lower level APIs. Very
probably, you can coerce them into doing what you want, though you might have probably, you can coerce them into doing what you want, though you might have
to write try/catch blocks (the horror!) and call process.exit() yourself, and to write try/catch blocks (the horror!) and call process.exit() yourself, and
Expand All @@ -59,24 +59,24 @@ Why?


As if Node didn't have enough option parsers. As of this writing, most of them As if Node didn't have enough option parsers. As of this writing, most of them
are almost good enough for me. None of them quite measures up to the power of are almost good enough for me. None of them quite measures up to the power of
perl's Getopt::Long though. Getopt::Long's interface sucks, but its parser is perl's yanop::Long though. yanop::Long's interface sucks, but its parser is
very flexible. This module aims to have an interface that doesn't suck and very flexible. This module aims to have an interface that doesn't suck and
still be flexible, but you'll be the judge. still be flexible, but you'll be the judge.


Specification Specification
------------- -------------


The primary way you give information to getopt is through a specification The primary way you give information to yanop is through a specification
object. The keys are the names of targets (keys in the result object), and the object. The keys are the names of targets (keys in the result object), and the
values are objects with the following keys: values are objects with the following keys:


### type ### type


This tells getopt what kind of thing you're trying to parse. It absolutely This tells yanop what kind of thing you're trying to parse. It absolutely
must be one of the following - you cannot pass a string, and there is no must be one of the following - you cannot pass a string, and there is no
default. default.


#### getopt.flag (or getopt.bool) #### yanop.flag (or yanop.bool)


Just an "on" switch. It'll be true if it was passed, and false if it wasn't. Just an "on" switch. It'll be true if it was passed, and false if it wasn't.
All the following forms are valid: All the following forms are valid:
Expand All @@ -85,18 +85,18 @@ All the following forms are valid:
-v -a -x -v -a -x
--verbose --anthropomorphic --xenophobic --verbose --anthropomorphic --xenophobic


#### getopt.scalar (or getopt.string) #### yanop.scalar (or yanop.string)


Expects one (and only one) value. If present at all, its value will be some Expects one (and only one) value. If present at all, its value will be some
kind of string. Passing more than one argument for options of this type will kind of string. Passing more than one argument for options of this type will
make getopt throw an error. The following forms are all valid: make yanop throw an error. The following forms are all valid:


-a42 -a42
-a 42 -a 42
--answer 42 --answer 42
--answer=42 --answer=42


#### getopt.array (or getopt.list) #### yanop.array (or yanop.list)


Expects zero or more values. You'll get an array in the result object of all Expects zero or more values. You'll get an array in the result object of all
the strings passed for this argument. the strings passed for this argument.
Expand All @@ -105,14 +105,14 @@ the strings passed for this argument.
*** ***
{ foo: ['one', 'two', 'three'] } { foo: ['one', 'two', 'three'] }


#### getopt.object (or getopt.hash) #### yanop.object (or yanop.hash)


This one is a little odd: like list, you can pass it multiple times, but the This one is a little odd: like list, you can pass it multiple times, but the
result will be an object (or hash) instead of an array, and the value will be result will be an object (or hash) instead of an array, and the value will be
split into key/value on the = sign. An example will probably explain better: split into key/value on the = sign. An example will probably explain better:


define: { define: {
type: getopt.object, type: yanop.object,
short: '-D' short: '-D'
} }
*** ***
Expand All @@ -125,7 +125,7 @@ split into key/value on the = sign. An example will probably explain better:
Either a string or an array of strings. All must be one character long (leave Either a string or an array of strings. All must be one character long (leave
off the -). This creates short aliases for your argument. The target will be off the -). This creates short aliases for your argument. The target will be
the same, though, and aliases can be mixed. In addition, for flag type shorts, the same, though, and aliases can be mixed. In addition, for flag type shorts,
they can be chained together. No short aliases are created by default. they can be chained together. No short aliases are created by default.


### long ### long


Expand All @@ -136,7 +136,7 @@ wish it to be a long alias.


### required ### required


A boolean. This causes getopt.parse to throw an exception if the argument A boolean. This causes yanop.parse to throw an exception if the argument
wasn't given, and is only valid for scalars and lists. In the list case, at wasn't given, and is only valid for scalars and lists. In the list case, at
least one value must be given. least one value must be given.


Expand All @@ -149,13 +149,13 @@ otherwise.
### description ### description


Purely optional, this should be a string explaining what your option does. The Purely optional, this should be a string explaining what your option does. The
help generator makes use of this -- see getopt.help() for details. help generator makes use of this -- see yanop.help() for details.


Positional Arguments Positional Arguments
-------------------- --------------------
Anything that doesn't look like an option will get aggregated into the result Anything that doesn't look like an option will get aggregated into the result
object's .argv property, in the order it was found in the actual argv. '-' is object's .argv property, in the order it was found in the actual argv. '-' is
treated as positional, and '--' will cause getopt to stop processing and treated as positional, and '--' will cause yanop to stop processing and
report the rest of the args as positional. report the rest of the args as positional.


Unrecognized Arguments Unrecognized Arguments
Expand All @@ -166,47 +166,47 @@ will cause an error to be thrown.
API API
=== ===


### getopt.parse(spec, argv) ### yanop.parse(spec, argv)


Processes argv without modifying it and returns a result object, which will Processes argv without modifying it and returns a result object, which will
have an argv property and other properties depending on the option spec. Any have an argv property and other properties depending on the option spec. Any
errors in parsing will throw an exception. If you don't specify argv, errors in parsing will throw an exception. If you don't specify argv,
process.argv (minus the first two elements) will be used. process.argv (minus the first two elements) will be used.


### getopt.tryParse(spec, argv) ### yanop.tryParse(spec, argv)


Wraps getopt.parse in a try/catch block. If exceptions are encountered, they Wraps yanop.parse in a try/catch block. If exceptions are encountered, they
are printed to stderr and the process will exit with a non-zero code. are printed to stderr and the process will exit with a non-zero code.


### getopt.zero() ### yanop.zero()


Returns the program name (e.g. "node myscript.js"). Useful when generating a Returns the program name (e.g. "node myscript.js"). Useful when generating a
usage message. usage message.


### getopt.usage() ### yanop.usage()


Uses getopt.zero() to generate a usage message of the form: Uses yanop.zero() to generate a usage message of the form:
"Usage: node myscript.js [OPTIONS]". This is the default message used by "Usage: node myscript.js [OPTIONS]". This is the default message used by
getopt.simple(). yanop.simple().


### getopt.help(spec) ### yanop.help(spec)


Returns a formatted string describing the options specified. Used internally Returns a formatted string describing the options specified. Used internally
by getopt.simple(), but you are encouraged to use it outside that context. by yanop.simple(), but you are encouraged to use it outside that context.


The description field of the spec is examined. If you didn't include a period The description field of the spec is examined. If you didn't include a period
at the end of the description, one will be added. Other bits of explanatory at the end of the description, one will be added. Other bits of explanatory
text (like "Required", "Default: " or "Can be specified multiple times") will text (like "Required", "Default: " or "Can be specified multiple times") will
be added to the end of the description to keep you from having to duplicate be added to the end of the description to keep you from having to duplicate
spec information in the description. spec information in the description.


### getopt.simple(spec, banner, argv) ### yanop.simple(spec, banner, argv)


Behaves similarly to getopt.tryParse(), except that it adds a "help" option Behaves similarly to yanop.tryParse(), except that it adds a "help" option
and then checks for it. If --help is passed, spec will be passed to and then checks for it. If --help is passed, spec will be passed to
getopt.help() to generate some the help. The given banner will be printed, yanop.help() to generate some the help. The given banner will be printed,
followed by this help. Banner and argv are both optional: banner defaults to followed by this help. Banner and argv are both optional: banner defaults to
getopt.usage(), and argv() defaults to process.argv as in getopt.parse(). yanop.usage(), and argv() defaults to process.argv as in yanop.parse().


Errors Errors
====== ======
Expand All @@ -218,18 +218,18 @@ useful to the user.
Any time you create a Parser or Help object (internal classes used by the api Any time you create a Parser or Help object (internal classes used by the api
methods above), an exception will be thrown if there is something inconsistent methods above), an exception will be thrown if there is something inconsistent
about your option specification. This helps you catch errors sooner. These about your option specification. This helps you catch errors sooner. These
exceptions are instances of getopt.SpecError, but you probably shouldn't try exceptions are instances of yanop.SpecError, but you probably shouldn't try
to catch them. getopt.tryParse et al will rethrow them. to catch them. yanop.tryParse et al will rethrow them.


Calling getopt.parse() an throw exceptions if the user has given bad options Calling yanop.parse() an throw exceptions if the user has given bad options
on the command line. These are generally the ones you want to try to catch and on the command line. These are generally the ones you want to try to catch and
print. They are all instances of getopt.ParseError. print. They are all instances of yanop.ParseError.


I think getopt should behave differently. I think yanop should behave differently.
========================================= =========================================


I value your opinion, I really do. I also have a job, and it isn't maintaining I value your opinion, I really do. I also have a job, and it isn't maintaining
getopt. Please, please either include a patch in your correspondance or send yanop. Please, please either include a patch in your correspondance or send
me a pull request on github. Otherwise, your issue may or may not be me a pull request on github. Otherwise, your issue may or may not be
addressed, but I'm gonna go out on a limb and say it probably won't be. Thanks addressed, but I'm gonna go out on a limb and say it probably won't be. Thanks
in advance for your contributions :) in advance for your contributions :)
Expand Down
8 changes: 4 additions & 4 deletions examples/simple.coffee
@@ -1,12 +1,12 @@
getopt = require 'getopt' yanop = require 'yanop'
sys = require 'sys' sys = require 'sys'


o = getopt.simple o = yanop.simple
munge: munge:
type: getopt.flag type: yanop.flag
description: 'Whether or not to munge' description: 'Whether or not to munge'
ickiness: ickiness:
type: getopt.scalar type: yanop.scalar
default: 1 default: 1
description: 'How icky to make the munging' description: 'How icky to make the munging'


Expand Down
2 changes: 1 addition & 1 deletion lib/exceptions.coffee
Expand Up @@ -4,7 +4,7 @@ exports.BaseError = class BaseError extends Error
@message = @describe() @message = @describe()


toString: () -> toString: () ->
'getopt.' + @constructor.name + ': ' + @describe() 'yanop.' + @constructor.name + ': ' + @describe()


describe: () -> describe: () ->
JSON.stringify(@desc) JSON.stringify(@desc)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion mkdist.pl
Expand Up @@ -25,5 +25,5 @@


my $tar = Archive::Tar->new; my $tar = Archive::Tar->new;
$tar->add_files(@MANIFEST); $tar->add_files(@MANIFEST);
my $name = "node-getopt-$version"; my $name = "yanop-$version";
$tar->write("$name.tar.gz", COMPRESS_GZIP, $name); $tar->write("$name.tar.gz", COMPRESS_GZIP, $name);
10 changes: 5 additions & 5 deletions package.json
@@ -1,10 +1,10 @@
{ {
"name" : "getopt", "name" : "yanop",
"description" : "Powerful, flexible command-line option parsing for NodeJS", "description" : "Yet Another Node Option Parser",
"version" : "0.1.0", "version" : "0.1.0",
"bugs" : "https://github.com/frodwith/node-getopt/issues", "bugs" : "https://github.com/frodwith/yanop/issues",
"keywords" : [ "option", "parse", "commandline", "command", "line" ], "keywords" : [ "option", "parse", "commandline", "command", "line" ],
"main" : "./lib/getopt.js", "main" : "./lib/yanop.js",
"directories" : { "directories" : {
"lib" : "./lib", "lib" : "./lib",
"test" : "./test" "test" : "./test"
Expand All @@ -24,7 +24,7 @@
"repositories" : [ "repositories" : [
{ {
"type" : "git", "type" : "git",
"url" : "http://github.com/frodwith/node-getopt" "url" : "http://github.com/frodwith/yanop"
} }
] ]
} }
14 changes: 7 additions & 7 deletions test/help.coffee
@@ -1,4 +1,4 @@
getopt = require '../lib/getopt' yanop = require '../lib/yanop'


expected = ''' expected = '''
-v -v
Expand All @@ -24,29 +24,29 @@ exports.basic = (t) ->
t.expect 1 t.expect 1
spec = spec =
verbose: verbose:
type: getopt.flag type: yanop.flag
short: 'v' short: 'v'
description: 'Print debugging messages' description: 'Print debugging messages'
output: output:
type: getopt.scalar type: yanop.scalar
short: 'o' short: 'o'
description: 'Filename (- for stdout) to write output to.' description: 'Filename (- for stdout) to write output to.'
default: '-' default: '-'
input: input:
type: getopt.list type: yanop.list
short: 'i' short: 'i'
description: 'Filename(s) (- for stdin) to read input from' description: 'Filename(s) (- for stdin) to read input from'
default: ['-'] default: ['-']
password: password:
type: getopt.scalar type: yanop.scalar
description: 'Secret string to use when connecting to server. This description is going to be ridiculously long so that we can test the line breaking a bit' description: 'Secret string to use when connecting to server. This description is going to be ridiculously long so that we can test the line breaking a bit'
required: true required: true
symbols: symbols:
type: getopt.hash type: yanop.hash
short: 'D', short: 'D',
long: 'define' long: 'define'
description: 'Symbols to define during processing' description: 'Symbols to define during processing'
u = new getopt.Help spec u = new yanop.Help spec
console.log(u.toString()) console.log(u.toString())
t.equals u.toString(), expected t.equals u.toString(), expected
t.done() t.done()

0 comments on commit 3d81dbd

Please sign in to comment.