Very basic command parser

Recently I created a small module that can read simple commands from a file or the keyboard, so that you can customise the run of a program. Here is a small example of how you can use it (actually the test program for the module):

program test_cmdparse
    use cmdparse

    type(cmdparser) :: parser

    character(len=40), dimension(4) :: cmds = &
        [ 'report-file (a)filename   ',   &
          'repeat (i)times           ',   &
          'allocate (i)chunks (i)size',   &
          'set-random (i)times       '    ]

    character(len=40)               :: cmd, string
    integer                         :: idx
    integer                         :: i, intvalue

    call parser%commands( cmds )
    call parser%set_input( "test.inp" )

    do while ( parser%has_next() )
        call parser%next( idx, cmd )
        write( *, * ) trim(cmd), ':'
        do i = 1,2
            call parser%get_arg( i, string )
            call parser%get_arg( i, intvalue )
            write( *, * ) idx, string, intvalue
        enddo
    enddo

    call parser%use_keyboard( .true., 'what do you want to do? ' )
    if ( parser%has_next() ) then
        call parser%next( idx, cmd )
        write( *, * ) trim(cmd), ':'
    endif
end program test_cmdparse

I have not put it in a repository yet, but will do so in the next few days, when I have time for it.

Mind you, it is very simple, very straightforward and completely incomplete, but it might be useful, as it was useful for me ;).

6 Likes