tool_command Subroutine

public subroutine tool_command(prog, args, output, command, argv, tokenized, input)

Build prog [-c input] args -o output both as a shell command and as an argv list, splitting prog and args with the OS-native lexer. tokenized is .false. when a part cannot be split: only the shell command can run then.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: prog

Program, possibly followed by its own flags

character(len=*), intent(in) :: args

Arguments for the program

character(len=*), intent(in) :: output

Output file, passed with -o

character(len=:), intent(out), allocatable :: command

Shell command

type(string_t), intent(out), allocatable :: argv(:)

Arguments for run_argv

logical, intent(out) :: tokenized

Whether every part could be split

character(len=*), intent(in), optional :: input

Optional source file, passed with -c


Source Code

subroutine tool_command(prog, args, output, command, argv, tokenized, input)
    !> Program, possibly followed by its own flags
    character(len=*), intent(in) :: prog
    !> Arguments for the program
    character(len=*), intent(in) :: args
    !> Output file, passed with -o
    character(len=*), intent(in) :: output
    !> Shell command
    character(len=:), allocatable, intent(out) :: command
    !> Arguments for run_argv
    type(string_t), allocatable, intent(out) :: argv(:)
    !> Whether every part could be split
    logical, intent(out) :: tokenized
    !> Optional source file, passed with -c
    character(len=*), optional, intent(in) :: input

    command = prog
    tokenized = .true.
    call split_append(argv, prog, .false., .false., tokenized)

    if (present(input)) then
        command = command // " -c " // input
        if (tokenized) then
            call add_strings(argv, string_t("-c"))
            call add_strings(argv, string_t(input))
        end if
    end if

    command = command // " " // args // " -o " // output
    if (tokenized .and. len_trim(args) > 0) call split_append(argv, args, .true., .true., tokenized)
    if (tokenized) then
        call add_strings(argv, string_t("-o"))
        call add_strings(argv, string_t(output))
    end if

end subroutine tool_command