|
| 1 | +# Print a typical cli program help message |
| 2 | +function print_help() |
| 3 | + name = basename(@__FILE__) |
| 4 | + io = stdout |
| 5 | + printstyled(io, "NAME\n", bold=true) |
| 6 | + println(io, " LiveServer.main - run a webserver") |
| 7 | + println(io) |
| 8 | + printstyled(io, "SYNOPSIS\n", bold=true) |
| 9 | + println(io, " julia -m LiveServer [-h <host>] [-p <port>] [-v] [--help] <directory>") |
| 10 | + println(io) |
| 11 | + printstyled(io, "DESCRIPTION\n", bold=true) |
| 12 | + println(io, """ `LiveServer.main` (typically invoked as `julia -m LiveServer`) |
| 13 | + starts a web server serving the contents of the specified |
| 14 | + filesystem directory using the LiveServer.jl Julia package. |
| 15 | + """) |
| 16 | + printstyled(io, "OPTIONS\n", bold=true) |
| 17 | + println(io, " <directory>") |
| 18 | + println(io, " Path to the root directory of the server (default: pwd)") |
| 19 | + println(io, " -h <host>") |
| 20 | + println(io, " Specify the host (default: 127.0.0.1)") |
| 21 | + println(io, " -p <port>") |
| 22 | + println(io, " Specify the port (default: 8000)") |
| 23 | + println(io, " -v") |
| 24 | + println(io, " Enable verbose output") |
| 25 | + println(io, " --help") |
| 26 | + println(io, " Show this message") |
| 27 | + return |
| 28 | +end |
| 29 | + |
| 30 | +function (@main)(ARGS) |
| 31 | + # Argument defaults |
| 32 | + port::Int = 8000 |
| 33 | + host::String = "127.0.0.1" |
| 34 | + verbose::Bool = false |
| 35 | + dir::String = pwd() |
| 36 | + # Argument parsing |
| 37 | + see_help = " See the output of `--help` for usage details." |
| 38 | + while length(ARGS) > 0 |
| 39 | + x = popfirst!(ARGS) |
| 40 | + if x == "-p" |
| 41 | + # Parse the port |
| 42 | + if length(ARGS) == 0 |
| 43 | + printstyled(stderr, "ERROR: "; bold=true, color=:red) |
| 44 | + println(stderr, "A port number is required after the `-p` flag.", see_help) |
| 45 | + return 1 |
| 46 | + end |
| 47 | + pstr = popfirst!(ARGS) |
| 48 | + p = tryparse(Int, pstr) |
| 49 | + if p === nothing |
| 50 | + printstyled(stderr, "ERROR: "; bold=true, color=:red) |
| 51 | + println(stderr, "Could not parse port number from input `$(pstr)`.", see_help) |
| 52 | + return 1 |
| 53 | + end |
| 54 | + port = p |
| 55 | + elseif x == "-h" |
| 56 | + # Parse the host |
| 57 | + if length(ARGS) == 0 |
| 58 | + printstyled(stderr, "ERROR: "; bold=true, color=:red) |
| 59 | + println(stderr, "A host is required after the `-h` flag.", see_help) |
| 60 | + return 1 |
| 61 | + end |
| 62 | + host = popfirst!(ARGS) |
| 63 | + elseif x == "-v" |
| 64 | + # Parse the verbose option |
| 65 | + verbose = true |
| 66 | + elseif x == "--help" |
| 67 | + # Print help and return (even if other arguments are present) |
| 68 | + print_help() |
| 69 | + return 0 |
| 70 | + elseif length(ARGS) == 0 && isdir(abspath(x)) |
| 71 | + # If ARGS is empty and the argument is a directory this is the root directory |
| 72 | + dir = abspath(x) |
| 73 | + else |
| 74 | + # Unknown argument |
| 75 | + printstyled(stderr, "ERROR: "; bold=true, color=:red) |
| 76 | + println(stderr, "Argument `$x` is not a supported flag or filesystem directory.", see_help) |
| 77 | + return 1 |
| 78 | + end |
| 79 | + end |
| 80 | + # Start the server |
| 81 | + LiveServer.serve(; host, port, dir, verbose) |
| 82 | + return 0 |
| 83 | +end |
0 commit comments