Skip to content

Instantly share code, notes, and snippets.

@hideshi
Created December 1, 2015 00:09
Show Gist options
  • Save hideshi/2683b72e0fa908d46fde to your computer and use it in GitHub Desktop.
Save hideshi/2683b72e0fa908d46fde to your computer and use it in GitHub Desktop.
コマンドラインツールの作り方 ref: http://qiita.com/hideshi@github/items/b93920901d76a471fcb3
#!/usr/bin/env elixir
System.argv()
|> Enum.each(fn(x) -> IO.puts x end)
#!/usr/bin/env elixir
System.argv()
|> Enum.map(fn(x) -> String.to_integer x end)
|> Enum.sum
|> IO.puts
#!/usr/bin/env elixir
{result, 0} = System.cmd "ls", ["-la"]
result
|> String.split("\n")
|> Enum.filter(fn(x) -> x =~ ~r/\.exs/ end)
|> Enum.each(fn(x) -> IO.puts x end)
$ chmod +x hello.exs
$ ./hello.exs
Hello world!
$ ./cmd.exs |grep calc
-rwxr-xr-x 1 hideshi staff 105 12 1 07:01 calc.exs
$ chmod +x opt.exs
$ ./opt.exs -x --debug --verbose -c foo bar
{[debug: true, verbose: true], ["bar"], [{"-x", nil}, {"-c", "foo"}]}
$ chmod +x argv.exs
$ ./argv.exs foo bar
foo
bar
$ chmod +x calc.exs
$ ./calc.exs 1 2 3 4
10
$ chmod +x pipe.exs
$ ls -la | ./pipe.exs
-rwxr-xr-x 1 hideshi staff 73 12 1 06:58 args.exs
-rwxr-xr-x 1 hideshi staff 105 12 1 07:01 calc.exs
-rwxr-xr-x 1 hideshi staff 44 12 1 06:33 hello.exs
-rwxr-xr-x 1 hideshi staff 138 12 1 07:11 pipe.exs
$ chmod +x cmd.exs
$ ./cmd.exs
-rwxr-xr-x 1 hideshi staff 73 12 1 06:58 args.exs
-rwxr-xr-x 1 hideshi staff 105 12 1 07:01 calc.exs
-rwxr-xr-x 1 hideshi staff 171 12 1 07:54 cmd.exs
-rwxr-xr-x 1 hideshi staff 44 12 1 06:33 hello.exs
-rwxr-xr-x 1 hideshi staff 138 12 1 07:11 pipe.exs
#!/usr/bin/env elixir
IO.puts "Hello world!"
#!/usr/bin/env elixir
System.argv
|> OptionParser.parse
|> IO.inspect
#!/usr/bin/env elixir
IO.stream(:stdio, :line)
|> Enum.filter(fn(x) -> x =~ ~r/\.exs/ end)
|> Enum.each(fn(x) -> IO.write(:stdio, x) end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment