Skip to content

Commit eec3d5b

Browse files
authored
Add at-main support to invoke LiveServer from the command line. (#179)
Julia 1.11 and later have the option to invoke a package's `main` function with the `-m` flag, e.g. `julia -m LiveServer <args>...`. This patch adds a main function to LiveServer. Output of `julia -m LiveServer --help`: ``` NAME LiveServer.main - run a webserver SYNOPSIS julia -m LiveServer [-h <host>] [-p <port>] [-v] [--help] <directory> DESCRIPTION `LiveServer.main` (typically invoked as `julia -m LiveServer`) starts a web server serving the contents of the specified filesystem directory using the LiveServer.jl Julia package. OPTIONS <directory> Path to the root directory of the server (default: pwd) -h <host> Specify the host (default: 127.0.0.1) -p <port> Specify the port (default: 8000) -v Enable verbose output --help Show this message ```
1 parent f51991c commit eec3d5b

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/LiveServer.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ include("server.jl")
5151

5252
include("utils.jl")
5353

54+
# CLI invocation with -m (@main) requires Julia >= 1.11
55+
if isdefined(Base, Symbol("@main"))
56+
include("main.jl")
57+
end
58+
5459
end # module

src/main.jl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)