parse_command_line Subroutine

public subroutine parse_command_line()

Parse some basic command line arguments. Currently just 'version' and 'help'.

This should be called before anything else, but especially before initialising 1.

Arguments

None

Contents

Source Code


Source Code

  subroutine parse_command_line()
    use git_version_mod, only: get_git_version
    use build_config, only : formatted_build_config
    use ingen_mod, only : run_ingen
    integer :: arg_count, arg_n
    integer :: arg_length
    character(len=:), allocatable :: argument
    character(len=*), parameter :: nl = new_line('a')
    character(len=*), parameter :: usage = &
         "gs2 [--version|-v] [--help|-h] [--build-config] [--check-input] [input file]" // nl // nl // &
         "GS2: A fast, flexible physicist's toolkit for gyrokinetics" // nl // &
         "For more help, see the documentation at https://gyrokinetics.gitlab.io/gs2/" // nl // &
         "or create an issue https://bitbucket.org/gyrokinetics/gs2/issues?status=open" // nl // &
         nl // &
         "  -h, --help           Print this message" // nl // &
         "  -v, --version        Print the GS2 version" // nl // &
         "  --build-config       Print the current build configuration" // nl // &
         "  --check-input FILE   Check input FILE for errors"

    arg_count = command_argument_count()

    do arg_n = 0, arg_count
      call get_command_argument(arg_n, length=arg_length)
      if (allocated(argument)) deallocate(argument)
      allocate(character(len=arg_length)::argument)
      call get_command_argument(arg_n, argument)

      if ((argument == "--help") .or. (argument == "-h")) then
        write(*, '(a)') usage
        stop
      else if ((argument == "--version") .or. (argument == "-v")) then
        write(*, '("GS2 version ", a)') get_git_version()
        stop
      else if (argument == "--build-config") then
        write(*, '(a)') formatted_build_config()
        stop
      else if (argument == "--check-input") then
        ! Next argument should be the input file
        if (arg_n == arg_count) then
          error stop "Missing input file for '--check-input'" // nl // usage
        end if

        call get_command_argument(arg_n + 1, length=arg_length)
        deallocate(argument)
        allocate(character(len=arg_length)::argument)
        call get_command_argument(arg_n + 1, argument)

        call run_ingen(input_file=argument)
        stop
      else if (argument == "--") then
        exit
      else if (argument(1:1) == "-") then
        write(*, '(a)') "Error: Unrecognised argument '" // argument // "'. Usage is:" // nl // nl // usage
        stop 2
      end if
    end do
  end subroutine parse_command_line