# OptionParser : conditional option

**URL:** <https://forum.crystal-lang.org/t/optionparser-conditional-option/8544>\
**Category:** Help & Support\
**Created:** [November 16, 2025, 6:43pm UTC](https://forum.crystal-lang.org/t/optionparser-conditional-option/8544 "2025-11-16T18:43:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [November 16, 2025, 6:43pm UTC](https://forum.crystal-lang.org/t/optionparser-conditional-option/8544/1 "2025-11-16T18:43:14Z")

</div>

Hi,  
In OptionParser, would it be possible to have an option B depending on another option A, giving an error if option A is missing?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [November 16, 2025, 7:31pm UTC](https://forum.crystal-lang.org/t/optionparser-conditional-option/8544/2 "2025-11-16T19:31:20Z")

</div>

Prob would be easiest to just handle it in your code after option parsing. E.g. `raise "Missing option A" if b && !a`?

---

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [November 16, 2025, 7:50pm UTC](https://forum.crystal-lang.org/t/optionparser-conditional-option/8544/3 "2025-11-16T19:50:25Z")

</div>

Yes, that’s what I thought too, but I was wondering if there was a trick with OptionParser.

---

<div class="post-metadata">

**Author:** ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)\
**Post date:** [November 17, 2025, 1:56pm UTC](https://forum.crystal-lang.org/t/optionparser-conditional-option/8544/4 "2025-11-17T13:56:02Z")

</div>

I’m a bit curious about how you’d like to define command-line option dependencies.

If you want to handle everything only with OptionParser, would it look like this?

```cr
parser = OptionParser.new do |p|
  p.on("-a", "--alpha", "Enable alpha") do
    has_a = true
  end

  p.on("-b VALUE", "--beta=VALUE", "Beta option (requires -a)") do |v|
    unless has_a
      STDERR.puts "ERROR: --beta requires --alpha"
      STDERR.puts p
      exit(1)
    end
    value_b = v
  end

```

---

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [November 18, 2025, 3:53pm UTC](https://forum.crystal-lang.org/t/optionparser-conditional-option/8544/5 "2025-11-18T15:53:23Z")

</div>

Yes, excellent suggestion.  
Thanks
