Created
December 1, 2015 00:09
-
-
Save hideshi/2683b72e0fa908d46fde to your computer and use it in GitHub Desktop.
コマンドラインツールの作り方 ref: http://qiita.com/hideshi@github/items/b93920901d76a471fcb3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env elixir | |
System.argv() | |
|> Enum.each(fn(x) -> IO.puts x end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env elixir | |
System.argv() | |
|> Enum.map(fn(x) -> String.to_integer x end) | |
|> Enum.sum | |
|> IO.puts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ chmod +x hello.exs | |
$ ./hello.exs | |
Hello world! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./cmd.exs |grep calc | |
-rwxr-xr-x 1 hideshi staff 105 12 1 07:01 calc.exs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ chmod +x opt.exs | |
$ ./opt.exs -x --debug --verbose -c foo bar | |
{[debug: true, verbose: true], ["bar"], [{"-x", nil}, {"-c", "foo"}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ chmod +x argv.exs | |
$ ./argv.exs foo bar | |
foo | |
bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ chmod +x calc.exs | |
$ ./calc.exs 1 2 3 4 | |
10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env elixir | |
IO.puts "Hello world!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env elixir | |
System.argv | |
|> OptionParser.parse | |
|> IO.inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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