|
| 1 | +using Pkg |
| 2 | +Pkg.activate(@__DIR__) |
| 3 | +Pkg.instantiate() |
| 4 | + |
| 5 | +using JuliaFormatter |
| 6 | + |
| 7 | +help = """ |
| 8 | +Usage: flux_format.jl [flags] [FILE/PATH]... |
| 9 | +
|
| 10 | +Formats the given julia files using the Flux formatting options. |
| 11 | +If paths are given instead, it will format all *.jl files under |
| 12 | +the paths. If nothing is given, all changed julia files are formatted. |
| 13 | +
|
| 14 | + -v, --verbose |
| 15 | + Print the name of the files being formatted with relevant details. |
| 16 | +
|
| 17 | + -h, --help |
| 18 | + Print this help message. |
| 19 | +""" |
| 20 | + |
| 21 | +options = Dict{Symbol, Bool}() |
| 22 | +indices_to_remove = [] # used to delete options once processed |
| 23 | + |
| 24 | +for (index, arg) in enumerate(ARGS) |
| 25 | + if arg[1] != '-' |
| 26 | + continue |
| 27 | + end |
| 28 | + if arg in ["-v", "--verbose"] |
| 29 | + opt = :verbose |
| 30 | + push!(indices_to_remove, index) |
| 31 | + elseif arg in ["-h", "--help"] |
| 32 | + opt = :help |
| 33 | + push!(indices_to_remove, index) |
| 34 | + else |
| 35 | + error("Option $arg is not supported.") |
| 36 | + end |
| 37 | + options[opt] = true |
| 38 | +end |
| 39 | + |
| 40 | +# remove options from args |
| 41 | +deleteat!(ARGS, indices_to_remove) |
| 42 | + |
| 43 | +# print help message if asked |
| 44 | +if haskey(options, :help) |
| 45 | + write(stdout, help) |
| 46 | + exit(0) |
| 47 | +end |
| 48 | + |
| 49 | +# otherwise format files |
| 50 | +if isempty(ARGS) |
| 51 | + filenames = readlines(`git ls-files "*.jl"`) |
| 52 | +else |
| 53 | + filenames = ARGS |
| 54 | +end |
| 55 | + |
| 56 | +write(stdout, "Formatting in progress.\n") |
| 57 | +format(filenames; options...) |
0 commit comments