<commit_before>require: "lib/option_parser"

FancySpec describe: OptionParser with: {
  it: "parses an option with no argument" when: {
    parsed_option? = false

    o = OptionParser new: @{
      with: "--test" doc: "Do something" do: {
        parsed_option? = true
      }
    }

    parsed_option? is: false
    o parse: ["foo", "bar", "--test", "baz"]
    parsed_option? is: true
  }

  it: "parses no option if none are passed" when: {
    parsed_option? = false
    o = OptionParser new: @{
      with: "--foo [arg]" doc: "bla" do: |arg| {
        parsed_option? = true
      }
      with: "--bar" doc: "more bla" do: {
        parsed_option? = true
      }
    }

    parsed_option? is: false
    o parse: ["hello", "foo", "bar", "no expected options given!"]
    parsed_option? is: false
  }

  it: "parses an option with an argument" when: {
    parsed_option? = false
    passed_args = []

    o = OptionParser new: @{
      with: "--my-val [arg]" doc: "gimme an argument!" do: |arg| {
        parsed_option? = true
        passed_args << (arg to_i)
      }
      with: "--my-other-val [arg]" doc: "gimme another argument!" do: |arg| {
        parsed_option? = true
        passed_args << (arg to_s * 2)
      }
    }

    parsed_option? is: false
    passed_args is: []
    o parse: ["foo", "1", "2", "bar", "--my-val", "42", "and", "--my-other-val", "42", "some", "more"]
    parsed_option? is: true
    passed_args size is: 2
    passed_args includes?: 42 . is: true
    passed_args includes?: "4242" . is: true
    o parsed_options is: <["--my-val" => "42", "--my-other-val" => "42"]>
  }

  it: "always prints the banner and --help message" with: 'print_help_info when: {
    o = OptionParser new: @{
      banner: "My Banner, yo"
    }
    out = StringIO new
    let: '*stdout* be: out in: {
      o print_help_info
      out string is: "My Banner, yo\n\n  --help  Display this information\n"
    }
  }

  it: "removes options and their arguments after they have been parsed" with: 'remove_after_parsed: when: {
    o = OptionParser new: @{
      remove_after_parsed: true
      with: "-value [arg]" doc: "" do: |arg| {}
      with: "--my-flag" doc: "" do: {}
    }
    args = [1, "-value", "foo", "--my-flag", 2]
    o parse: args
    args is: [1, 2]
  }
}
<commit_msg>specify parse: method for OptionParser tests<commit_after>require: "lib/option_parser"

FancySpec describe: OptionParser with: {
  it: "parses an option with no argument" with: 'parse: when: {
    parsed_option? = false

    o = OptionParser new: @{
      with: "--test" doc: "Do something" do: {
        parsed_option? = true
      }
    }

    parsed_option? is: false
    o parse: ["foo", "bar", "--test", "baz"]
    parsed_option? is: true
  }

  it: "parses no option if none are passed" with: 'parse: when: {
    parsed_option? = false
    o = OptionParser new: @{
      with: "--foo [arg]" doc: "bla" do: |arg| {
        parsed_option? = true
      }
      with: "--bar" doc: "more bla" do: {
        parsed_option? = true
      }
    }

    parsed_option? is: false
    o parse: ["hello", "foo", "bar", "no expected options given!"]
    parsed_option? is: false
  }

  it: "parses an option with an argument" with: 'parse: when: {
    parsed_option? = false
    passed_args = []

    o = OptionParser new: @{
      with: "--my-val [arg]" doc: "gimme an argument!" do: |arg| {
        parsed_option? = true
        passed_args << (arg to_i)
      }
      with: "--my-other-val [arg]" doc: "gimme another argument!" do: |arg| {
        parsed_option? = true
        passed_args << (arg to_s * 2)
      }
    }

    parsed_option? is: false
    passed_args is: []
    o parse: ["foo", "1", "2", "bar", "--my-val", "42", "and", "--my-other-val", "42", "some", "more"]
    parsed_option? is: true
    passed_args size is: 2
    passed_args includes?: 42 . is: true
    passed_args includes?: "4242" . is: true
    o parsed_options is: <["--my-val" => "42", "--my-other-val" => "42"]>
  }

  it: "always prints the banner and --help message" with: 'print_help_info when: {
    o = OptionParser new: @{
      banner: "My Banner, yo"
    }
    out = StringIO new
    let: '*stdout* be: out in: {
      o print_help_info
      out string is: "My Banner, yo\n\n  --help  Display this information\n"
    }
  }

  it: "removes options and their arguments after they have been parsed" with: 'remove_after_parsed: when: {
    o = OptionParser new: @{
      remove_after_parsed: true
      with: "-value [arg]" doc: "" do: |arg| {}
      with: "--my-flag" doc: "" do: {}
    }
    args = [1, "-value", "foo", "--my-flag", 2]
    o parse: args
    args is: [1, 2]
  }
}

{'fraction_non_alphanumeric': 0.16039561384648462, 'fraction_numerical': 0.009460331111588905, 'mean_word_length': 2.39067055393586, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '3270683', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1635, 'n_words': 515}