Creating a CLI app in Julia

I have to share my code in a way that my non-programming colleagues can use it on their own. Currently there is a Julia function which accepts a path to an Excel file, containing all data and metadata, does processing, creates a folder and puts result files therein. My personal usage is a script file in VSCode, where I just (re-)write the path as a parameter. Installing VSCode on their Win10 computers and teaching them how to run such a script is a feasible option.

Another option is a CLI application. Besides ArgParse.jl , a short search found Comonicon.jl , Fire.jl , and maurice . I am not sure I understand all usages, but it seems in each case the Julia script would be executed once with the provided arguments, meaning Julia re-start for each input. Well, waiting 10…30s should be acceptable.

But I’d prefer a CLI app processing multiple inputs in a loop like this:

> mdeq.bat
provide the file please
mdeq>C:\somepath\experimentXX.xlsx
processed OK
provide the file please
mdeq>C:\somepath\experimentYY.xlsx
provide the file please
mdeq>exit
>

Sure, not that difficult to write from the scratch, but can it be that nobody has done that before?

1 Like
while true
    print("Enter filename or empty line to finish: ")
    fname = readline(stdin)
    isempty(fname) && break
    do_computation(fname)
    println("Completed processing $fname")
end
7 Likes

Thank you, that indeed would be sufficient in my case (will only have to add activating the proper environment).

Still, just out of curiosity: E.g. Fire.jl offers a number of practical options. Maybe there exists something like that, but in a loop, ready to use?

NativeFileDialog.jl is also nice for this:

using NativeFileDialog: pick_file

while true
    fname = pick_file(homedir(), filterlist = "xlsx")
    isempty(fname) && break # user cancelled

    do_computation(fname)
    println("Completed processing $fname")
end
5 Likes

GitHub - MasonProtter/ReplMaker.jl: Simple API for building repl modes in Julia is also a really nice tool for making CLIs with line editing and with options to have history and tab completion (mostly for the benefit of others reading this thread as it is probably not ideal for your situation).

3 Likes

I’m leaving my job, and have to provide an access to my Julia software for my colleagues. Here I document how I do it - in case someone may use it, too, or maybe suggest improvements.

I’ve copied the package onto our file server. Colleagues installed juiaup onto their (Windows) computers and executed:

juliaup add 1.10
juliaup default 1.10

Julia version in the Project.toml is pinned to 1.10

[compat]
julia = "~1.10"

The package folder is copied onto a user computer, and before the first use, the file instantiate.bat , sitting at the root of the project folder, is executed in terminal:

julia %~dp0src\instantiate.jl 

%~dp0 being the path to the bat file

The instantiate.jl file:

using Pkg
basedir = splitdir(@__DIR__)[1]
Pkg.activate(basedir)
Pkg.instantiate()

To start program, use the file main.bat

julia %~dp0src\main.jl 

main.jl :

using Pkg
basedir = splitdir(@__DIR__)[1]
Pkg.activate(basedir)

using Mdes # my package
# using NativeFileDialog: pick_file # moved now into Mdes.jl

repl()

repl.jl :

function repl()
    imgtype = nothing
    while true
        println("Select the plots output format")
        println("Enter s for SVG, p for PDF, or just press ENTER for PNG")
        t = readline(stdin)
        t = t |> strip |> uppercase
        if isempty(t)
            imgtype="png"
        elseif t == "S"
            imgtype="svg"
        elseif t == "P"
            imgtype = "pdf"
        else
            println("sorry, cannot understand your input")
        end
        !isnothing(imgtype) && break
    end

    while true
        fname = pick_file(homedir(), filterlist = "xlsx")
        isempty(fname) && break # user cancelled
        try
            fit_all_and_save(fname; imgtype)
        catch ex
            println("Something went wrong: $ex")
        end
        println("Completed processing $fname")
    end
    return nothing
end
1 Like

I think that you made a typo, your colleagues might not know what a PFD file is.

1 Like

Edited - thank you :slight_smile:

1 Like