Skip to content

Commit

Permalink
move to v0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ylxdzsw committed Jul 12, 2018
1 parent a87c9a0 commit 0af03d8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 34 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ os:
- osx
- linux
julia:
- 0.5
- 0.6
- nightly
script:
- julia -e 'Pkg.clone(pwd()); Pkg.test("Fire")'
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ Fire.jl

[![](http://pkg.julialang.org/badges/Fire_0.5.svg)](http://pkg.julialang.org/?pkg=Fire)
[![](http://pkg.julialang.org/badges/Fire_0.6.svg)](http://pkg.julialang.org/?pkg=Fire)
[![](http://pkg.julialang.org/badges/Fire_0.7.svg)](http://pkg.julialang.org/?pkg=Fire)

Fire.jl is a library for creating simple CLI from julia function definitions. Inspired by [python-fire](https://github.com/google/python-fire).
Fire.jl is a library for creating simple CLI from julia function definitions.

### Installation

Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
julia 0.5
julia 0.7-
26 changes: 13 additions & 13 deletions src/Fire.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Base.Meta: quot

export @main

immutable FuncDef
struct FuncDef
name::Symbol
args::Vector
kwargs::Vector
Expand All @@ -25,7 +25,7 @@ const VARLENGTH = 0x03

const entries = FuncDef[]
const typecache = Dict(1=>String)
const typecount = ((c) -> () -> c += 1)(1)
const typecount = (c -> () -> c += 1)(1)

function __init__()
atexit(parse_command_line)
Expand Down Expand Up @@ -101,12 +101,12 @@ function parse_command_line()
pargs, oargs = [], Dict{Symbol, Any}()
entry = if length(entries) > 1
isempty(ARGS) && return print_help_all()
cmd = shift!(ARGS)
cmd = popfirst!(ARGS)
if cmd == "--help"
return print_help_all()
else
i = findfirst(x->x.name == Symbol(cmd), entries)
i == 0 && return println(STDERR, "ERROR: unknown command $cmd, see --help for more avaliable commands")
i == 0 && return println(stderr, "ERROR: unknown command $cmd, see --help for more avaliable commands")
entries[i]
end
else
Expand All @@ -122,7 +122,7 @@ function parse_command_line()
i = 1

while !isempty(ARGS)
x = shift!(ARGS)
x = popfirst!(ARGS)
if startswith(x, "--")
if x == "--help"
return print_help_entry(entry)
Expand All @@ -131,7 +131,7 @@ function parse_command_line()
x = Symbol(x[3:end])

arg = let ind = findfirst(y->y[1] == x, entry.kwargs)
ind != 0 ? entry.kwargs[ind] : return println(STDERR, "Unknown option --$x, see --help for avaliable options")
ind != 0 ? entry.kwargs[ind] : return println(stderr, "Unknown option --$x, see --help for avaliable options")
end

if arg[2] == Bool
Expand All @@ -140,26 +140,26 @@ function parse_command_line()
oargs[arg[1]] = arg[2]()
while !isempty(ARGS)
x = ARGS[1]
startswith(x, "--") ? break : shift!(ARGS)
startswith(x, "--") ? break : popfirst!(ARGS)
push!(oargs[arg[1]], parse_with_type(eltype(arg[2]), x))
end
elseif arg[2] in BASIC_TYPES
if isempty(ARGS) || (x = shift!(ARGS); startswith(x, "--"))
return println(STDERR, "Option --$(arg[1]) need an argument")
if isempty(ARGS) || (x = popfirst!(ARGS); startswith(x, "--"))
return println(stderr, "Option --$(arg[1]) need an argument")
end

oargs[arg[1]] = parse_with_type(arg[2], x)
else
return println(STDERR, "Argument type $(arg[2]) is not supported by Fire.jl")
return println(stderr, "Argument type $(arg[2]) is not supported by Fire.jl")
end
else
if i > length(entry.args)
return println(STDERR, "Too many arguments, see --help for list of arguments")
return println(stderr, "Too many arguments, see --help for list of arguments")
end

arg = entry.args[i]

arg[2] in BASIC_TYPES || return println(STDERR, "Argument type $(arg[2]) is not supported by Fire.jl")
arg[2] in BASIC_TYPES || return println(stderr, "Argument type $(arg[2]) is not supported by Fire.jl")

push!(pargs, parse_with_type(arg[2], x))

Expand All @@ -172,7 +172,7 @@ function parse_command_line()
number_of_required_arguments = count(x->x[3]==REQUIRED, entry.args)

if length(pargs) < number_of_required_arguments
return println(STDERR, "Need $number_of_required_arguments positional arguments, see --help for what are them")
return println(stderr, "Need $number_of_required_arguments positional arguments, see --help for what are them")
end

entry.func(pargs...; oargs...)
Expand Down
24 changes: 7 additions & 17 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
using Base.Test
if !isdefined(Base.Test, Symbol("@test_nowarn"))
macro test_nowarn(x)
esc(x)
end
end
if !isdefined(Base.Test, Symbol("@test_warn"))
macro test_warn(x, y)
esc(y)
end
end
using Test

const test_file = let
dir, cnt = mktempdir(), 0

function test_file(code)
local function test_file(code)
filename = joinpath(dir, "test$cnt.jl")

open(filename, "w") do f
Expand Down Expand Up @@ -97,7 +87,7 @@ end
@testset "splat" begin
f = test_file("""
@main function main(x::Integer, y::Integer...)
println(x * +(y...))
println(x * +(y...,))
end
""")

Expand All @@ -114,10 +104,10 @@ end
""")

@test begin
a = readstring(`julia $f --help`)
contains(a, "this is the doc string") &&
contains(a, "default: 3") &&
contains(a, "--y: Integer")
a = read(`julia $f --help`, String)
all(("this is the doc string", "default: 3", "--y: Integer")) do x
occursin(x, a)
end
end
end

Expand Down

0 comments on commit 0af03d8

Please sign in to comment.