2010-05-04 23:31:28 -04:00
<!DOCTYPE html> < html > < head > < title > optparse.coffee< / title > < meta http-equiv = "content-type" content = "text/html; charset=UTF-8" > < link rel = "stylesheet" media = "all" href = "docco.css" / > < / head > < body > < div id = "container" > < div id = "background" > < / div > < div id = "jump_to" > Jump To … < div id = "jump_wrapper" > < div id = "jump_page" > < a class = "source" href = "cake.html" > cake.coffee < / a > < a class = "source" href = "coffee-script.html" > coffee-script.coffee < / a > < a class = "source" href = "command.html" > command.coffee < / a > < a class = "source" href = "grammar.html" > grammar.coffee < / a > < a class = "source" href = "helpers.html" > helpers.coffee < / a > < a class = "source" href = "index.html" > index.coffee < / a > < a class = "source" href = "lexer.html" > lexer.coffee < / a > < a class = "source" href = "nodes.html" > nodes.coffee < / a > < a class = "source" href = "optparse.html" > optparse.coffee < / a > < a class = "source" href = "repl.html" > repl.coffee < / a > < a class = "source" href = "rewriter.html" > rewriter.coffee < / a > < a class = "source" href = "scope.html" > scope.coffee < / a > < / div > < / div > < / div > < table cellpadding = "0" cellspacing = "0" > < thead > < tr > < th class = "docs" > < h1 > optparse.coffee < / h1 > < / th > < th class = "code" > < / th > < / tr > < / thead > < tbody > < tr id = "section-1" > < td class = "docs" > < div class = "octowrap" > < a class = "octothorpe" href = "#section-1" > #< / a > < / div > < p > A simple < strong > OptionParser< / strong > class to parse option flags from the command-line.
2010-03-07 13:41:15 -05:00
Use it like so:< / p >
2010-03-06 23:48:06 -05:00
2010-08-03 00:06:34 -04:00
< pre > < code > parser = new OptionParser switches, helpBanner
options = parser.parse process.argv
2010-07-11 10:40:44 -04:00
< / code > < / pre >
< p > The first non-option is considered to be the start of the file (and file
2010-08-04 23:14:34 -04:00
option) list, and all subsequent arguments are left unparsed.< / p > < / td > < td class = "code" > < div class = "highlight" > < pre > < span class = "nv" > exports.OptionParser = < / span > < span class = "nx" > class< / span > < span class = "nx" > OptionParser< / span > < / pre > < / div > < / td > < / tr > < tr id = "section-2" > < td class = "docs" > < div class = "octowrap" > < a class = "octothorpe" href = "#section-2" > #< / a > < / div > < p > Initialize with a list of valid options, in the form:< / p >
2010-03-07 13:41:15 -05:00
< pre > < code > [short-flag, long-flag, description]
< / code > < / pre >
2010-08-04 23:14:34 -04:00
< p > Along with an an optional banner for the usage help.< / p > < / td > < td class = "code" > < div class = "highlight" > < pre > < span class = "nx" > constructor< / span > < span class = "o" > :< / span > < span class = "p" > (< / span > < span class = "nx" > rules< / span > < span class = "p" > ,< / span > < span class = "nx" > banner< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span >
< span class = "vi" > @banner = < / span > < span class = "nx" > banner< / span >
< span class = "vi" > @rules = < / span > < span class = "nx" > buildRules< / span > < span class = "nx" > rules< / span > < / pre > < / div > < / td > < / tr > < tr id = "section-3" > < td class = "docs" > < div class = "octowrap" > < a class = "octothorpe" href = "#section-3" > #< / a > < / div > < p > Parse the list of arguments, populating an < code > options< / code > object with all of the
2010-03-07 13:41:15 -05:00
specified options, and returning it. < code > options.arguments< / code > will be an array
containing the remaning non-option arguments. This is a simpler API than
many option parsers that allow you to attach callback actions for every
2010-08-04 23:14:34 -04:00
flag. Instead, you're responsible for interpreting the options object.< / p > < / td > < td class = "code" > < div class = "highlight" > < pre > < span class = "nx" > parse< / span > < span class = "o" > :< / span > < span class = "p" > (< / span > < span class = "nx" > args< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span >
< span class = "nv" > options = < / span > < span class = "nx" > arguments< / span > < span class = "o" > :< / span > < span class = "p" > []< / span >
< span class = "nv" > args = < / span > < span class = "nx" > normalizeArguments< / span > < span class = "nx" > args< / span >
2010-07-11 10:40:44 -04:00
< span class = "k" > for< / span > < span class = "nx" > arg< / span > < span class = "p" > ,< / span > < span class = "nx" > i< / span > < span class = "k" > in< / span > < span class = "nx" > args< / span >
2010-08-04 23:14:34 -04:00
< span class = "nv" > isOption = < / span > < span class = "o" > !!< / span > < span class = "p" > (< / span > < span class = "nx" > arg< / span > < span class = "p" > .< / span > < span class = "nx" > match< / span > < span class = "p" > (< / span > < span class = "nx" > LONG_FLAG< / span > < span class = "p" > )< / span > < span class = "o" > or< / span > < span class = "nx" > arg< / span > < span class = "p" > .< / span > < span class = "nx" > match< / span > < span class = "p" > (< / span > < span class = "nx" > SHORT_FLAG< / span > < span class = "p" > ))< / span >
< span class = "nv" > matchedRule = < / span > < span class = "kc" > no< / span >
2010-04-03 12:01:46 -04:00
< span class = "k" > for< / span > < span class = "nx" > rule< / span > < span class = "k" > in< / span > < span class = "nx" > @rules< / span >
2010-06-28 00:19:58 -04:00
< span class = "k" > if< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > shortFlag< / span > < span class = "o" > is< / span > < span class = "nx" > arg< / span > < span class = "o" > or< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > longFlag< / span > < span class = "o" > is< / span > < span class = "nx" > arg< / span >
2010-08-11 00:40:15 -04:00
< span class = "nv" > value = < / span > < span class = "k" > if< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > hasArgument< / span > < span class = "k" > then< / span > < span class = "nx" > args< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "o" > +=< / span > < span class = "mi" > 1< / span > < span class = "p" > ]< / span > < span class = "k" > else< / span > < span class = "kc" > true< / span >
< span class = "nx" > options< / span > < span class = "p" > [< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > name< / span > < span class = "p" > ]< / span > < span class = "o" > =< / span > < span class = "k" > if< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > isList< / span > < span class = "k" > then< / span > < span class = "p" > (< / span > < span class = "nx" > options< / span > < span class = "p" > [< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > name< / span > < span class = "p" > ]< / span > < span class = "o" > or< / span > < span class = "p" > []).< / span > < span class = "nx" > concat< / span > < span class = "nx" > value< / span > < span class = "k" > else< / span > < span class = "nx" > value< / span >
2010-08-04 23:14:34 -04:00
< span class = "nv" > matchedRule = < / span > < span class = "kc" > yes< / span >
2010-03-06 23:48:06 -05:00
< span class = "k" > break< / span >
2010-08-11 00:40:15 -04:00
< span class = "k" > throw< / span > < span class = "k" > new< / span > < span class = "nb" > Error< / span > < span class = "s2" > " unrecognized option: #{arg}" < / span > < span class = "k" > if< / span > < span class = "nx" > isOption< / span > < span class = "o" > and< / span > < span class = "o" > not< / span > < span class = "nx" > matchedRule< / span >
2010-07-11 10:40:44 -04:00
< span class = "k" > if< / span > < span class = "o" > not< / span > < span class = "nx" > isOption< / span >
2010-08-04 23:14:34 -04:00
< span class = "nv" > options.arguments = < / span > < span class = "nx" > args< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ...< / span > < span class = "nx" > args< / span > < span class = "p" > .< / span > < span class = "nx" > length< / span > < span class = "p" > ]< / span >
2010-07-11 10:40:44 -04:00
< span class = "k" > break< / span >
2010-03-07 13:41:15 -05:00
< span class = "nx" > options< / span > < / pre > < / div > < / td > < / tr > < tr id = "section-4" > < td class = "docs" > < div class = "octowrap" > < a class = "octothorpe" href = "#section-4" > #< / a > < / div > < p > Return the help text for this < strong > OptionParser< / strong > , listing and describing all
2010-08-04 23:14:34 -04:00
of the valid options, for < code > --help< / code > and such.< / p > < / td > < td class = "code" > < div class = "highlight" > < pre > < span class = "nx" > help< / span > < span class = "o" > :< / span > < span class = "o" > -> < / span >
< span class = "nv" > lines = < / span > < span class = "p" > [< / span > < span class = "s1" > ' Available options:' < / span > < span class = "p" > ]< / span >
2010-08-11 00:40:15 -04:00
< span class = "nx" > lines< / span > < span class = "p" > .< / span > < span class = "nx" > unshift< / span > < span class = "s2" > " #{@banner}\n" < / span > < span class = "k" > if< / span > < span class = "nx" > @banner< / span >
2010-04-03 12:01:46 -04:00
< span class = "k" > for< / span > < span class = "nx" > rule< / span > < span class = "k" > in< / span > < span class = "nx" > @rules< / span >
2010-08-04 23:14:34 -04:00
< span class = "nv" > spaces = < / span > < span class = "mi" > 15< / span > < span class = "o" > -< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > longFlag< / span > < span class = "p" > .< / span > < span class = "nx" > length< / span >
< span class = "nv" > spaces = < / span > < span class = "k" > if< / span > < span class = "nx" > spaces< / span > < span class = "o" > > < / span > < span class = "mi" > 0< / span > < span class = "k" > then< / span > < span class = "p" > (< / span > < span class = "s1" > ' ' < / span > < span class = "k" > for< / span > < span class = "nx" > i< / span > < span class = "k" > in< / span > < span class = "p" > [< / span > < span class = "mi" > 0< / span > < span class = "p" > ..< / span > < span class = "nx" > spaces< / span > < span class = "p" > ]).< / span > < span class = "nx" > join< / span > < span class = "p" > (< / span > < span class = "s1" > ' ' < / span > < span class = "p" > )< / span > < span class = "k" > else< / span > < span class = "s1" > ' ' < / span >
< span class = "nv" > letPart = < / span > < span class = "k" > if< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > shortFlag< / span > < span class = "k" > then< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > shortFlag< / span > < span class = "o" > +< / span > < span class = "s1" > ' , ' < / span > < span class = "k" > else< / span > < span class = "s1" > ' ' < / span >
2010-08-11 00:40:15 -04:00
< span class = "nx" > lines< / span > < span class = "p" > .< / span > < span class = "nx" > push< / span > < span class = "s1" > ' ' < / span > < span class = "o" > +< / span > < span class = "nx" > letPart< / span > < span class = "o" > +< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > longFlag< / span > < span class = "o" > +< / span > < span class = "nx" > spaces< / span > < span class = "o" > +< / span > < span class = "nx" > rule< / span > < span class = "p" > .< / span > < span class = "nx" > description< / span >
2010-08-04 23:14:34 -04:00
< span class = "s2" > " \n#{ lines.join(' \n' ) }\n" < / span > < / pre > < / div > < / td > < / tr > < tr id = "section-5" > < td class = "docs" > < div class = "octowrap" > < a class = "octothorpe" href = "#section-5" > #< / a > < / div > < h2 > Helpers< / h2 > < / td > < td class = "code" > < div class = "highlight" > < pre > < / pre > < / div > < / td > < / tr > < tr id = "section-6" > < td class = "docs" > < div class = "octowrap" > < a class = "octothorpe" href = "#section-6" > #< / a > < / div > < p > Regex matchers for option flags.< / p > < / td > < td class = "code" > < div class = "highlight" > < pre > < span class = "nv" > LONG_FLAG = < / span > < span class = "sr" > /^(--\w[\w\-]+)/< / span >
< span class = "nv" > SHORT_FLAG = < / span > < span class = "sr" > /^(-\w)/< / span >
< span class = "nv" > MULTI_FLAG = < / span > < span class = "sr" > /^-(\w{2,})/< / span >
2010-08-11 00:40:15 -04:00
< span class = "nv" > OPTIONAL = < / span > < span class = "sr" > /\[(\w+(\*?))\]/< / span > < / pre > < / div > < / td > < / tr > < tr id = "section-7" > < td class = "docs" > < div class = "octowrap" > < a class = "octothorpe" href = "#section-7" > #< / a > < / div > < p > Build and return the list of option rules. If the optional < em > short-flag< / em > is
2010-08-04 23:14:34 -04:00
unspecified, leave it out by padding with < code > null< / code > .< / p > < / td > < td class = "code" > < div class = "highlight" > < pre > < span class = "nv" > buildRules = < / span > < span class = "p" > (< / span > < span class = "nx" > rules< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span >
2010-03-06 23:48:06 -05:00
< span class = "k" > for< / span > < span class = "nx" > tuple< / span > < span class = "k" > in< / span > < span class = "nx" > rules< / span >
< span class = "nx" > tuple< / span > < span class = "p" > .< / span > < span class = "nx" > unshift< / span > < span class = "kc" > null< / span > < span class = "k" > if< / span > < span class = "nx" > tuple< / span > < span class = "p" > .< / span > < span class = "nx" > length< / span > < span class = "o" > < < / span > < span class = "mi" > 3< / span >
2010-06-28 00:19:58 -04:00
< span class = "nx" > buildRule< / span > < span class = "nx" > tuple< / span > < span class = "p" > ...< / span > < / pre > < / div > < / td > < / tr > < tr id = "section-8" > < td class = "docs" > < div class = "octowrap" > < a class = "octothorpe" href = "#section-8" > #< / a > < / div > < p > Build a rule from a < code > -o< / code > short flag, a < code > --output [DIR]< / code > long flag, and the
2010-08-11 00:40:15 -04:00
description of what the option does.< / p > < / td > < td class = "code" > < div class = "highlight" > < pre > < span class = "nv" > buildRule = < / span > < span class = "p" > (< / span > < span class = "nx" > shortFlag< / span > < span class = "p" > ,< / span > < span class = "nx" > longFlag< / span > < span class = "p" > ,< / span > < span class = "nx" > description< / span > < span class = "p" > ,< / span > < span class = "nx" > options< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span >
< span class = "nv" > match = < / span > < span class = "nx" > longFlag< / span > < span class = "p" > .< / span > < span class = "nx" > match< / span > < span class = "p" > (< / span > < span class = "nx" > OPTIONAL< / span > < span class = "p" > )< / span >
< span class = "nv" > longFlag = < / span > < span class = "nx" > longFlag< / span > < span class = "p" > .< / span > < span class = "nx" > match< / span > < span class = "p" > (< / span > < span class = "nx" > LONG_FLAG< / span > < span class = "p" > )[< / span > < span class = "mi" > 1< / span > < span class = "p" > ]< / span >
< span class = "nx" > options< / span > < span class = "o" > or=< / span > < span class = "p" > {}< / span >
2010-03-06 23:48:06 -05:00
< span class = "p" > {< / span >
2010-08-04 23:14:34 -04:00
< span class = "nx" > name< / span > < span class = "o" > :< / span > < span class = "nx" > longFlag< / span > < span class = "p" > .< / span > < span class = "nx" > substr< / span > < span class = "mi" > 2< / span >
< span class = "nx" > shortFlag< / span > < span class = "o" > :< / span > < span class = "nx" > shortFlag< / span >
< span class = "nx" > longFlag< / span > < span class = "o" > :< / span > < span class = "nx" > longFlag< / span >
< span class = "nx" > description< / span > < span class = "o" > :< / span > < span class = "nx" > description< / span >
< span class = "nx" > hasArgument< / span > < span class = "o" > :< / span > < span class = "o" > !!< / span > < span class = "p" > (< / span > < span class = "nx" > match< / span > < span class = "o" > and< / span > < span class = "nx" > match< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > ])< / span >
2010-08-11 00:40:15 -04:00
< span class = "nx" > isList< / span > < span class = "o" > :< / span > < span class = "o" > !!< / span > < span class = "p" > (< / span > < span class = "nx" > match< / span > < span class = "o" > and< / span > < span class = "nx" > match< / span > < span class = "p" > [< / span > < span class = "mi" > 2< / span > < span class = "p" > ])< / span >
2010-03-07 13:41:15 -05:00
< span class = "p" > }< / span > < / pre > < / div > < / td > < / tr > < tr id = "section-9" > < td class = "docs" > < div class = "octowrap" > < a class = "octothorpe" href = "#section-9" > #< / a > < / div > < p > Normalize arguments by expanding merged flags into multiple flags. This allows
2010-08-04 23:14:34 -04:00
you to have < code > -wl< / code > be the same as < code > --watch --lint< / code > .< / p > < / td > < td class = "code" > < div class = "highlight" > < pre > < span class = "nv" > normalizeArguments = < / span > < span class = "p" > (< / span > < span class = "nx" > args< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span >
< span class = "nv" > args = < / span > < span class = "nx" > args< / span > < span class = "p" > .< / span > < span class = "nx" > slice< / span > < span class = "mi" > 0< / span >
< span class = "nv" > result = < / span > < span class = "p" > []< / span >
2010-03-06 23:48:06 -05:00
< span class = "k" > for< / span > < span class = "nx" > arg< / span > < span class = "k" > in< / span > < span class = "nx" > args< / span >
2010-08-04 23:14:34 -04:00
< span class = "k" > if< / span > < span class = "nv" > match = < / span > < span class = "nx" > arg< / span > < span class = "p" > .< / span > < span class = "nx" > match< / span > < span class = "nx" > MULTI_FLAG< / span >
2010-03-06 23:48:06 -05:00
< span class = "nx" > result< / span > < span class = "p" > .< / span > < span class = "nx" > push< / span > < span class = "s1" > ' -' < / span > < span class = "o" > +< / span > < span class = "nx" > l< / span > < span class = "k" > for< / span > < span class = "nx" > l< / span > < span class = "k" > in< / span > < span class = "nx" > match< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > ].< / span > < span class = "nx" > split< / span > < span class = "s1" > ' ' < / span >
< span class = "k" > else< / span >
< span class = "nx" > result< / span > < span class = "p" > .< / span > < span class = "nx" > push< / span > < span class = "nx" > arg< / span >
< span class = "nx" > result< / span >
< / pre > < / div > < / td > < / tr > < / tbody > < / table > < / div > < / body > < / html >