File: demo_specified.f90

package info (click to toggle)
fortran-cli2 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 25,856 kB
  • sloc: f90: 6,172; javascript: 3,423; makefile: 188; sh: 25
file content (77 lines) | stat: -rwxr-xr-x 2,528 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
     program demo_specified
     use, intrinsic :: iso_fortran_env, only : &
     & stderr=>ERROR_UNIT, stdin=>INPUT_UNIT, stdout=>OUTPUT_UNIT
     use M_CLI2,  only : set_args, igets, rgets, specified, sget, lget
     implicit none

     ! Define args
     integer,allocatable  :: ints(:)
     real,allocatable     :: floats(:)
     logical              :: flag
     character(len=:),allocatable :: color
     character(len=:),allocatable :: list(:)
     integer :: i

      call set_args('&
         & --color:c "red"       &
         & --flag:f F            &
         & --ints:i 1,10,11      &
         & --floats:T 12.3, 4.56 &
         & ')
      ints=igets('ints')
      floats=rgets('floats')
      flag=lget('flag')
      color=sget('color')

      write(*,*)'color=',color
      write(*,*)'flag=',flag
      write(*,*)'ints=',ints
      write(*,*)'floats=',floats

      write(*,*)'was -flag specified?',specified('flag')

      ! elemental
      write(*,*)specified(['floats','ints  '])

      ! If you want to know if groups of parameters were specified use
      ! ANY(3f) and ALL(3f)
      write(*,*)'ANY:',any(specified(['floats','ints  ']))
      write(*,*)'ALL:',all(specified(['floats','ints  ']))

      ! For mutually exclusive
      if (all(specified(['floats','ints  '])))then
          write(*,*)'You specified both names --ints and --floats'
      endif

      ! For required parameter
      if (.not.any(specified(['floats','ints  '])))then
          write(*,*)'You must specify --ints or --floats'
      endif

     ! check if all values are in range from 10 to 30 and even
     write(*,*)'are all numbers good?',all([ints >= 10,ints <= 30,(ints/2)*2 == ints])

     ! perhaps you want to check one value at a time
     do i=1,size(ints)
        write(*,*)ints(i),[ints(i) >= 10,ints(i) <= 30,(ints(i)/2)*2 == ints(i)]
        if(all([ints(i) >= 10,ints(i) <= 30,(ints(i)/2)*2 == ints(i)]) )then
           write(*,*)ints(i),'is an even number from 10 to 30 inclusive'
        else
           write(*,*)ints(i),'is not an even number from 10 to 30 inclusive'
        endif
     enddo

     list = [character(len=10) :: 'red','white','blue']
     if( any(color == list) )then
        write(*,*)color,'matches a value in the list'
     else
        write(*,*)color,'not in the list'
     endif

     if(size(ints).eq.3)then
        write(*,*)'ints(:) has expected number of values'
     else
        write(*,*)'ints(:) does not have expected number of values'
     endif

     end program demo_specified