From 4b30306691c2c4c623871aa663a0e8083b4b11fc Mon Sep 17 00:00:00 2001 From: elijah Date: Sat, 23 Feb 2013 20:51:41 -0800 Subject: added `rake doc` command. --- lib/leap_cli.rb | 2 + lib/leap_cli/app.rb | 57 ------------ lib/leap_cli/markdown_document_listener.rb | 134 +++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 57 deletions(-) delete mode 100644 lib/leap_cli/app.rb create mode 100644 lib/leap_cli/markdown_document_listener.rb (limited to 'lib') diff --git a/lib/leap_cli.rb b/lib/leap_cli.rb index 384a5f2..714211a 100644 --- a/lib/leap_cli.rb +++ b/lib/leap_cli.rb @@ -24,6 +24,8 @@ require 'leap_cli/config/secrets' require 'leap_cli/config/object_list' require 'leap_cli/config/manager' +require 'leap_cli/markdown_document_listener' + module LeapCli::Commands; end # diff --git a/lib/leap_cli/app.rb b/lib/leap_cli/app.rb deleted file mode 100644 index 90c4ae9..0000000 --- a/lib/leap_cli/app.rb +++ /dev/null @@ -1,57 +0,0 @@ -require 'gli' -require 'highline' -require 'forwardable' -require 'lib_ext/gli' # our custom extensions to gli - -# -# Typically, GLI and Highline methods are loaded into the global namespace. -# Instead, here we load these into the module LeapCli::Commands in order to -# ensure that the cli logic and code is kept isolated to leap_cli/commands/*.rb -# -# no cheating! -# -module LeapCli::Commands - extend GLI::App - extend Forwardable - - # - # delegate highline methods to make them available to sub-commands - # - @terminal = HighLine.new - def_delegator :@terminal, :ask, 'self.ask' - def_delegator :@terminal, :agree, 'self.agree' - def_delegator :@terminal, :choose, 'self.choose' - def_delegator :@terminal, :say, 'self.say' - def_delegator :@terminal, :color, 'self.color' - def_delegator :@terminal, :list, 'self.list' - - # - # make config manager available as 'manager' - # - def self.manager - @manager ||= begin - manager = LeapCli::Config::Manager.new - manager.load - manager - end - end - - # - # info about leap command line suite - # - program_desc LeapCli::SUMMARY - program_long_desc LeapCli::DESCRIPTION - - # - # handle --version ourselves - # - if ARGV.grep(/--version/).any? - puts "leap #{LeapCli::VERSION}, ruby #{RUBY_VERSION}" - exit(0) - end - - # - # load commands and run - # - commands_from('leap_cli/commands') -end diff --git a/lib/leap_cli/markdown_document_listener.rb b/lib/leap_cli/markdown_document_listener.rb new file mode 100644 index 0000000..60b012e --- /dev/null +++ b/lib/leap_cli/markdown_document_listener.rb @@ -0,0 +1,134 @@ +# +# A class to generate a markdown file with all the information available with the +# help subcommand. +# +# This is adapted from GLI::Commands::RdocDocumentListener +# + +require 'stringio' +require 'gli/commands/help_modules/arg_name_formatter' + +module LeapCli + class MarkdownDocumentListener + + def initialize(global_options,options,arguments) + @io = File.new(File.basename($0) + ".md",'w') + @nest = '' + @commands = [File.basename($0)] + @arg_name_formatter = GLI::Commands::HelpModules::ArgNameFormatter.new + end + + def beginning + end + + # Called when processing has completed + def ending + @io.close + end + + # Gives you the program description + def program_desc(desc) + @io.puts "@title = 'Command Line Reference'" + #@io.puts "# #{File.basename($0)} - #{desc}" + @io.puts + end + + def program_long_desc(desc) + @io.puts desc + @io.puts + end + + # Gives you the program version + def version(version) + #@io.puts "v#{version}" + #@io.puts + end + + def options + #@io.puts "
" + @io.puts + if @nest.size == 0 + @io.puts "# Global Options" + else + #@io.puts "#{@nest}# Options" + @io.puts "**Options**" + end + @io.puts + end + + # Gives you a flag in the current context + def flag(name,aliases,desc,long_desc,default_value,arg_name,must_match,type) + invocations = ([name] + Array(aliases)).map { |_| add_dashes(_) }.join('|') + usage = "#{invocations} #{arg_name || 'arg'}" + #@io.puts "#{@nest}## #{usage}" + @io.puts "* `#{usage}` " + @io.puts String(desc).strip + " " + @io.puts String(long_desc).strip + " " if long_desc + @io.puts "Default Value: #{default_value || 'None'} " + @io.puts "Must Match: #{must_match.to_s} " unless must_match.nil? + @io.puts + end + + # Gives you a switch in the current context + def switch(name,aliases,desc,long_desc,negetable) + if negetable + name = "[no-]#{name}" if name.to_s.length > 1 + aliases = aliases.map { |_| _.to_s.length > 1 ? "[no-]#{_}" : _ } + end + invocations = ([name] + aliases).map { |_| add_dashes(_) }.join('|') + #@io.puts "#{@nest}## #{invocations}" + @io.puts "* `#{invocations}` " + @io.puts String(desc).strip + " " + #@io.puts + #@io.puts String(long_desc).strip + @io.puts + end + + def end_options + #@io.puts "
" + end + + def commands + #@io.puts "#{@nest}## Commands" + #@nest = "#{@nest}#" + end + + # Gives you a command in the current context and creates a new context of this command + def command(name,aliases,desc,long_desc,arg_name,arg_options) + @commands.push(name) + #@io.puts "#{@nest}## Command: #{([name] + aliases).join('|')} #{@arg_name_formatter.format(arg_name,arg_options)}" + @io.puts + @io.puts "#{@nest}# #{@commands.join ' '} #{@arg_name_formatter.format(arg_name,arg_options)}" + @io.puts + @io.puts String(desc).strip + @io.puts + @io.puts String(long_desc).strip + @nest = "#{@nest}#" + end + + # Ends a command, and "pops" you back up one context + def end_command(name) + @nest.gsub!(/\#$/,'') + @commands.pop + end + + # Gives you the name of the current command in the current context + def default_command(name) + @io.puts "Default Command: #{name}" unless name.nil? + end + + def end_commands + @nest.gsub!(/\#$/,'') + end + + private + + def add_dashes(name) + name = "-#{name}" + name = "-#{name}" if name.length > 2 + name + end + + + end +end -- cgit v1.2.3