parse_args Subroutine

public subroutine parse_args(args, cli, stat, errmsg)

Parse command-line arguments (without the program name).

Arguments

Type IntentOptional Attributes Name
type(string_t), intent(in) :: args(:)

the arguments, in order

type(cli_t), intent(out) :: cli

the parsed command line

integer, intent(out) :: stat

0, or EXIT_ERROR for a bad command line

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

what is wrong, if stat is nonzero


Calls

proc~~parse_args~~CallsGraph proc~parse_args parse_args proc~apply_setting apply_setting proc~parse_args->proc~apply_setting proc~to_s to_s proc~apply_setting->proc~to_s

Source Code

subroutine parse_args(args, cli, stat, errmsg)
    !! Parse command-line arguments (without the program name).
    type(string_t), intent(in) :: args(:)                   !! the arguments, in order
    type(cli_t), intent(out) :: cli                         !! the parsed command line
    integer, intent(out) :: stat                            !! 0, or `EXIT_ERROR` for a bad command line
    character(len=:), allocatable, intent(out) :: errmsg    !! what is wrong, if `stat` is nonzero
    type(format_options_t) :: scratch
    integer :: k, j
    character(len=:), allocatable :: arg
    logical :: only_paths

    stat = 0
    allocate (cli%settings(0), cli%paths(0), cli%exclude(0))
    cli%config = ''
    only_paths = .false.
    k = 0
    do while (k < size(args))
        k = k + 1
        arg = args(k)%s
        if (only_paths .or. len(arg) == 0 .or. arg == '-') then
            cli%paths = [cli%paths, string_t(arg)]
            cycle
        end if
        if (arg(1:1) /= '-') then
            cli%paths = [cli%paths, string_t(arg)]
            cycle
        end if
        select case (arg)
        case ('--')
            only_paths = .true.
        case ('-h', '--help')
            cli%help = .true.
        case ('--version')
            cli%version = .true.
        case ('-s', '--stdout')
            cli%stdout = .true.
        case ('-c', '--check')
            cli%check = .true.
        case ('-d', '--diff')
            cli%diff = .true.
        case ('-q', '--quiet')
            cli%quiet = .true.
        case ('--no-config')
            cli%no_config = .true.
        case ('--indent-units')
            call add('indent-units', 'true')
        case ('--indent-fypp')
            call add('indent-fypp', 'true')
        case ('--no-indent-procedures')
            call add('indent-procedures', 'false')
        case ('--no-indent-comments')
            call add('indent-comments', 'false')
        case ('--keep-column1-comments')
            call add('column1-comments', 'keep')
        case ('--no-split')
            call add('split', 'false')
        case ('--sort-use')
            call add('sort-use', 'true')
        case ('--sort-use-only')
            call add('sort-use-only', 'true')
        case default
            if (.not. takes_value(arg)) then
                call fail('unknown option '//arg)
                return
            end if
            if (k == size(args)) then
                call fail('missing value for '//arg)
                return
            end if
            k = k + 1
            call option_value(arg, args(k)%s)
            if (stat /= 0) return
        end select
    end do

    ! check the settings now, so that a mistake is reported before any file is changed
    do j = 1, size(cli%settings)
        call apply_setting(scratch, cli%settings(j)%key, cli%settings(j)%value, stat, errmsg)
        if (stat /= 0) then
            stat = EXIT_ERROR
            return
        end if
    end do
    if (.not. (cli%help .or. cli%version) .and. size(cli%paths) == 0) call fail('no files given')

contains

    logical function takes_value(opt)
        !! True if option `opt` (as written, e.g. `-i` or `--indent`) is followed by a value.
        character(len=*), intent(in) :: opt   !! the argument to test
        select case (opt)
        case ('-i', '--indent', '--base-indent', '--continuation', '--continuation-indent', '-l', '--line-length', '--relational', &
              '--spaces', '--case', '--set', '--config', '--exclude', '--extensions', '--comment-spacing')
            takes_value = .true.
        case default
            takes_value = .false.
        end select
    end function takes_value

    subroutine option_value(opt, value)
        !! Record option `opt`, one for which `takes_value` is true, with `value`, the argument after it.
        character(len=*), intent(in) :: opt     !! the option, as written
        character(len=*), intent(in) :: value   !! its value
        select case (opt)
        case ('-i', '--indent')
            call add('indent', value)
        case ('--base-indent')
            call add('base-indent', value)
        case ('--continuation')
            call add('continuation', value)
        case ('--continuation-indent')
            call add('continuation-indent', value)
        case ('--comment-spacing')
            call add('comment-spacing', value)
        case ('-l', '--line-length')
            call add('line-length', value)
        case ('--relational')
            call add('relational', value)
        case ('--spaces')
            call add_list('spaces.', value)
        case ('--case')
            call add_list('case.', value)
        case ('--set')
            call add_list('', value)
        case ('--config')
            cli%config = value
        case ('--exclude')
            cli%exclude = [cli%exclude, string_t(value)]
        case ('--extensions')
            call extension_list(value)
        end select
    end subroutine option_value

    subroutine add(key, value)
        !! Append the setting `key` with `value` to `cli%settings`.
        character(len=*), intent(in) :: key     !! setting name, as in a configuration file
        character(len=*), intent(in) :: value   !! its value as text
        cli%settings = [cli%settings, setting_t(key, value)]
    end subroutine add

    subroutine add_list(prefix, text)
        !! Comma-separated `NAME=VALUE` items.
        character(len=*), intent(in) :: prefix  !! prepended to each `NAME` to make the setting name, e.g. `spaces.`
        character(len=*), intent(in) :: text    !! the items
        integer :: i, j, eq
        character(len=:), allocatable :: item
        i = 1
        do while (i <= len(text))
            j = index(text(i:), ',')
            if (j == 0) then
                j = len(text) + 1
            else
                j = i + j - 1
            end if
            item = text(i:j - 1)
            eq = index(item, '=')
            if (eq == 0) then
                call fail('expected NAME=VALUE for '//arg//', not "'//item//'"')
                return
            end if
            call add(prefix//item(1:eq - 1), item(eq + 1:))
            i = j + 1
        end do
    end subroutine add_list

    subroutine extension_list(text)
        !! Set `cli%extensions` from the comma-separated list `text`, dropping a leading `.` of each
        !! extension and skipping empty items.
        character(len=*), intent(in) :: text   !! the value of `--extensions`
        integer :: i, j
        allocate (cli%extensions(0))
        i = 1
        do while (i <= len(text))
            j = index(text(i:), ',')
            if (j == 0) then
                j = len(text) + 1
            else
                j = i + j - 1
            end if
            if (j > i) then
                if (text(i:i) == '.') then
                    cli%extensions = [cli%extensions, string_t(text(i + 1:j - 1))]
                else
                    cli%extensions = [cli%extensions, string_t(text(i:j - 1))]
                end if
            end if
            i = j + 1
        end do
    end subroutine extension_list

    subroutine fail(msg)
        !! Make parsing fail with `EXIT_ERROR` and message `msg`.
        character(len=*), intent(in) :: msg   !! what is wrong
        stat = EXIT_ERROR
        errmsg = msg
    end subroutine fail

end subroutine parse_args