<commit_before>require 'optparse'

begin
  Module.const_get('BasicObject')
  # We are 1.9.x
rescue NameError
  BasicObject = Object
end

module Methadone
  # Include this module to gain access to the "canonical command-line app structure"
  # DSL.  This is a *very* lightweight layer on top of what you might
  # normally write that gives you just a bit of help to keep your code structured
  # in a sensible way.  You can use as much or as little as you want, though
  # you must at least use #main to get any benefits.
  #
  # You also get a more expedient interface to OptionParser as well
  # as checking for required arguments to your app.  For example, if
  # we want our app to accept a negatable switch named "switch", a flag
  # named "flag", and two arguments "needed" (which is required) 
  # and "maybe" which is optional, we can do the following:
  #
  #     #!/usr/bin/env ruby -w
  #       
  #     require 'methadone'
  #      
  #     include Methadone::Main
  #     
  #     main do |needed, maybe|
  #       options[:switch] => true or false, based on command line
  #       options[:flag] => value of flag passed on command line
  #     end
  #     
  #     # Proxy to an OptionParser instance's on method
  #     on("--[no]-switch")
  #     on("--flag VALUE")
  #
  #     arg :needed
  #     arg :maybe, :optional
  #     
  #     go!
  #
  # Our app then acts as follows:
  #
  #     $ our_app 
  #     # => parse error: 'needed' is required
  #     $ our_app foo
  #     # => succeeds; "maybe" in main is nil
  #
  # This also includes Methadone::CLILogging to give you access to simple logging
  module Main
    include Methadone::CLILogging
    # Declare the main method for your app.
    # This allows you to specify the general logic of your
    # app at the top of your bin file, but can rely on any methods
    # or other code that you define later.  
    #
    # For example, suppose you want to process a set of files, but
    # wish to determine that list from another method to keep your
    # code clean.
    #
    #     #!/usr/bin/env ruby -w
    #
    #     require 'methadone'
    #
    #     include Methadone::Main
    #
    #     main do
    #       files_to_process.each do |file|
    #         # process file
    #       end
    #     end
    #
    #     def files_to_process
    #       # return list of files
    #     end
    #
    #     go!
    #
    # The block can accept any parameters, and unparsed arguments
    # from the command line will be passed.
    # 
    # To run this method, call #go!
    def main(&block)
      @main_block = block
      @options = {}
      @option_parser = OptionParserProxy.new(OptionParser.new,@options)
    end

    # Configure the auto-handling of StandardError exceptions caught
    # from calling go!.
    #
    # leak - if true, go! will *not* catch StandardError exceptions, but instead
    #        allow them to bubble up.  If false, they will be caught and handled as normal.
    #        This does *not* affect Methadone::Error exceptions; those will NOT leak through.
    def leak_exceptions(leak)
      @leak_exceptions = leak
    end


    # Start your command-line app, exiting appropriately when
    # complete.
    #
    # This *will* exit your program when it completes.  If your
    # #main block evaluates to an integer, that value will be sent
    # to Kernel#exit, otherwise, this will exit with 0
    #
    # If the command-line options couldn't be parsed, this
    # will exit with 64 and whatever message OptionParser provided.
    #
    # If a required argument (see #arg) is not found, this exits with
    # 64 and a message about that missing argument.
    #
    def go!
      normalize_defaults
      opts.parse!
      opts.check_args!
      result = call_main
      if result.kind_of? Fixnum
        exit result
      else
        exit 0
      end
    rescue OptionParser::ParseError => ex
      error ex.message
      exit 64 # Linux standard for bad command line
    end

    # Call this to exit the program immediately
    # with the given error code and message.
    #
    # +exit_code+:: exit status you'd like to exit with
    # +message+:: message to display to the user explaining the problem
    def exit_now!(exit_code,message=nil)
      raise Methadone::Error.new(exit_code,message)
    end

    # Returns an OptionParser that you can use
    # to declare your command-line interface.  Generally, you
    # won't use this and will use #on directly, but this allows
    # you to have complete control of option parsing.
    #
    # The object returned has
    # an additional feature that implements typical use of OptionParser.
    #
    #     opts.on("--flag VALUE")<commit_msg>Show help when ran with no arguments<commit_after>require 'optparse'

begin
  Module.const_get('BasicObject')
  # We are 1.9.x
rescue NameError
  BasicObject = Object
end

module Methadone
  # Include this module to gain access to the "canonical command-line app structure"
  # DSL.  This is a *very* lightweight layer on top of what you might
  # normally write that gives you just a bit of help to keep your code structured
  # in a sensible way.  You can use as much or as little as you want, though
  # you must at least use #main to get any benefits.
  #
  # You also get a more expedient interface to OptionParser as well
  # as checking for required arguments to your app.  For example, if
  # we want our app to accept a negatable switch named "switch", a flag
  # named "flag", and two arguments "needed" (which is required) 
  # and "maybe" which is optional, we can do the following:
  #
  #     #!/usr/bin/env ruby -w
  #       
  #     require 'methadone'
  #      
  #     include Methadone::Main
  #     
  #     main do |needed, maybe|
  #       options[:switch] => true or false, based on command line
  #       options[:flag] => value of flag passed on command line
  #     end
  #     
  #     # Proxy to an OptionParser instance's on method
  #     on("--[no]-switch")
  #     on("--flag VALUE")
  #
  #     arg :needed
  #     arg :maybe, :optional
  #     
  #     go!
  #
  # Our app then acts as follows:
  #
  #     $ our_app 
  #     # => parse error: 'needed' is required
  #     $ our_app foo
  #     # => succeeds; "maybe" in main is nil
  #
  # This also includes Methadone::CLILogging to give you access to simple logging
  module Main
    include Methadone::CLILogging
    # Declare the main method for your app.
    # This allows you to specify the general logic of your
    # app at the top of your bin file, but can rely on any methods
    # or other code that you define later.  
    #
    # For example, suppose you want to process a set of files, but
    # wish to determine that list from another method to keep your
    # code clean.
    #
    #     #!/usr/bin/env ruby -w
    #
    #     require 'methadone'
    #
    #     include Methadone::Main
    #
    #     main do
    #       files_to_process.each do |file|
    #         # process file
    #       end
    #     end
    #
    #     def files_to_process
    #       # return list of files
    #     end
    #
    #     go!
    #
    # The block can accept any parameters, and unparsed arguments
    # from the command line will be passed.
    # 
    # To run this method, call #go!
    def main(&block)
      @main_block = block
      @options = {}
      @option_parser = OptionParserProxy.new(OptionParser.new,@options)
    end

    # Configure the auto-handling of StandardError exceptions caught
    # from calling go!.
    #
    # leak - if true, go! will *not* catch StandardError exceptions, but instead
    #        allow them to bubble up.  If false, they will be caught and handled as normal.
    #        This does *not* affect Methadone::Error exceptions; those will NOT leak through.
    def leak_exceptions(leak)
      @leak_exceptions = leak
    end


    # Start your command-line app, exiting appropriately when
    # complete.
    #
    # This *will* exit your program when it completes.  If your
    # #main block evaluates to an integer, that value will be sent
    # to Kernel#exit, otherwise, this will exit with 0
    #
    # If the command-line options couldn't be parsed, this
    # will exit with 64 and whatever message OptionParser provided.
    #
    # If a required argument (see #arg) is not found, this exits with
    # 64 and a message about that missing argument.
    #
    def go!
      normalize_defaults
      opts.parse!
      opts.check_args!
      result = call_main
      if result.kind_of? Fixnum
        exit result
      else
        exit 0
      end
    rescue OptionParser::ParseError => ex
      puts
      error ex.message
      puts
      puts opts.help
      exit 64 # Linux standard for bad command line
    end

    # Call this to exit the program immediately
    # with the given error code and message.
    #
    # +exit_code+:: exit status you'd like to exit with
    # +message+:: message to display to the user explaining the problem
    def exit_now!(exit_code,message=nil)
      raise Methadone::Error.new(exit_code,message)
    end

    # Returns an OptionParser that you can use
    # to declare your command-line interface.  Generally, you
    # won't use this and will use #on directly, but this allows
    # you to have complete control of option parsing.
    #
    # The object returned has
    # an additional feature that implements typical use of OptionParser.
    #
    #     opts.on("--flag VALUE")
{'fraction_non_alphanumeric': 0.07299809845763786, 'fraction_numerical': 0.0021128248468201987, 'mean_word_length': 2.282593619972261, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2810082', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2707, 'n_words': 1151}