Mandatory field even if not the first field

31 Views Asked by At

I have this command which will require the user to input the first field.

             CMD PROMPT('TEST')                                 
             PARM KWD(NAME) TYPE(*CHAR) LEN(10) PROMPT('NAME') +
                  MIN(01)                                       
             PARM KWD(TYPE) TYPE(*CHAR) LEN(05) PROMPT('TYPE') +
                  DFT(*OPT1) VALUES(*OPT1 *OPT2) RSTD(*YES)     

If I change the order into like this, then the NAME field is not required anymore.

             CMD PROMPT('TEST')                                 
             PARM KWD(TYPE) TYPE(*CHAR) LEN(05) PROMPT('TYPE') +
                  DFT(*OPT1) VALUES(*OPT1 *OPT2) RSTD(*YES)   
             PARM KWD(NAME) TYPE(*CHAR) LEN(10) PROMPT('NAME') +
                  MIN(01)                                       

Spool file will also show this: Required parameter not allowed after optional parameter. Optional parameter assumed.

Is there a way to make the 2nd field mandatory if the layout of the fields is like this or this is already a limitation?

2

There are 2 best solutions below

0
nfgl On BEST ANSWER

You can change the order they are displayed using PROMPT(... order)

Like below :

   CMD        PROMPT('TEST')
   PARM       KWD(NAME) TYPE(*CHAR) LEN(10) MIN(01) PROMPT('NAME' 2)
   PARM       KWD(TYPE) TYPE(*CHAR) LEN(05) RSTD(*YES) DFT(*OPT1) VALUES(*OPT1 *OPT2) +
         PROMPT('TYPE' 1) 
0
WStanzl On

This can (only) be solved with a Validity Checking Program.

Use CRTCMD CMD(TEST) ... VLDCKR(yourlib/TESTV) to create your command.

Validity Checking Programs are not difficult to make, and very useful because you can also check existence of objects, database contents etc. Below is a simple CL example. Sensibly you use the already-existing message CPD0071 from QSYS/QCPFMSG. The validity checking program must send CPF0002 with MSGTYPE(*ESCAPE) as the last message. This indicates to the command processor that it shall not let the command pass validly. If you have several CPD0071's to send, send only one CPF0002. In the msgdta of CPD0071, make sure you have four blanks before the parameter name, and the parameter name padded to ten bytes; it is a requirement of the message data for CPD0071.

Source for CL Program TESTV:

pgm parm(&type &name)                                                  
dcl &type *char 5                                                      
dcl &name *char 10                                                     
                                                                       
   if (&name *eq ' ') then(do)                                            
      sndpgmmsg msgid(cpd0071) msgf(qcpfmsg) +
                msgdta('    NAME      ') msgtype(*diag)
      sndpgmmsg  msgid(cpf0002) msgf(qcpfmsg) msgtype(*escape)            
   enddo                                                                  
                                                                       
endpgm