Skip to content

Instantly share code, notes, and snippets.

@hideshi
Created December 2, 2015 00:04
Show Gist options
  • Select an option

  • Save hideshi/261ccc96c94a7ed5a11b to your computer and use it in GitHub Desktop.

Select an option

Save hideshi/261ccc96c94a7ed5a11b to your computer and use it in GitHub Desktop.

Revisions

  1. hideshi created this gist Dec 2, 2015.
    4 changes: 4 additions & 0 deletions file1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    defmodule EightBall.CLI do
    def main(args) do
    end
    end
    1 change: 1 addition & 0 deletions file10.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    "Question must be a string, ending with a question mark."
    1 change: 1 addition & 0 deletions file11.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    mix escript.build
    4 changes: 4 additions & 0 deletions file12.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    ➜ eight_ball git:(master) ✗ mix escript.build
    Compiled lib/eight_ball.ex
    Generated eight_ball app
    Generated escript eight_ball with MIX_ENV=dev
    2 changes: 2 additions & 0 deletions file13.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    ➜ eight_ball git:(master) ✗ ./eight_ball --question "Is Elixir awesome?"
    Outlook good
    2 changes: 2 additions & 0 deletions file14.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    ➜ eight_ball git:(master) ✗ ./eight_ball --question "Is Elixir awesome"
    Question must be a string, ending with a question mark.
    1 change: 1 addition & 0 deletions file2.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    eight_ball --question "Is Elixir great?"
    1 change: 1 addition & 0 deletions file3.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    eight_ball -q "Is Elixir great?"
    9 changes: 9 additions & 0 deletions file4.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    defmodule EightBall.CLI do
    def main(argv) do
    {options, _, _} = OptionParser.parse(argv,
    switches: [question: :string],
    )

    IO.inspect options
    end
    end
    1 change: 1 addition & 0 deletions file5.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    mix escript.build
    4 changes: 4 additions & 0 deletions file6.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    ➜ eight_ball git:(master) ✗ mix escript.build
    Compiled lib/eight_ball/cli.ex
    Generated eight_ball app
    Generated escript eight_ball with MIX_ENV=dev
    1 change: 1 addition & 0 deletions file7.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ./eight_ball --question "Is Elixir great?"
    2 changes: 2 additions & 0 deletions file8.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    ➜ eight_ball git:(master) ✗ ./eight_ball -q "Is Elixir great?"
    [question: "Is Elixir great?"]
    15 changes: 15 additions & 0 deletions file9.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    defmodule EightBall.CLI do
    def main(opts) do
    {options, _, _} = OptionParser.parse(opts,
    switches: [question: :string],
    aliases: [q: :question] # makes '-q' an alias of '--question'
    )

    try do
    IO.puts EightBall.ask(options[:question])
    rescue
    e in RuntimeError -> e
    IO.puts e.message
    end
    end
    end
    15 changes: 15 additions & 0 deletions mix.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    defmodule EightBall.Mixfile do
    use Mix.Project

    def project do
    [app: :eight_ball,
    version: "0.0.1",
    elixir: "~> 1.0",
    build_embedded: Mix.env == :prod,
    start_permanent: Mix.env == :prod,
    escript: [main_module: EightBall.CLI], # <- this line
    deps: deps,
    package: package ]
    end
    # ...
    end