Parsing Command Line Arguments In PowerBuilder
Arguments Parsing In PowerBuilder.
Ex:
parsecmd.exe -A Avalue -B Bvalue -C Cvalue
parsecmd.exe –A Avalue –B Bvalue -C Cvalue
parsecmd.exe /A Avalue /B Bvalue /C Cvalue
parsecmd.exe -A “Avalue” –B Bvalue /C “Cvalue”
Parse Argument:
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
//==================================================================== // Function: n_parse_command.of_parse() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_arg //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2021/03/13 //-------------------------------------------------------------------- // Usage: n_parse_command.of_parse ( string as_arg ) //-------------------------------------------------------------------- // Modify History: // //==================================================================== Integer li_argcnt, li_arg, li_argnew String ls_arg[], ls_argline, ls_cmd Long ll_pos Boolean lb_key String ls_prefixkey is_arg = ls_arg ls_cmd = Trim(as_arg) If IsNull(ls_cmd) Or Len(Trim(ls_cmd)) = 0 Then ii_numerror = -3 Return -3 End If li_argcnt = 0 Do While Len(ls_cmd ) > 0 li_argcnt++ // Check for quotes If Left(ls_cmd, 1) = "'" Then // Find the end single quote ll_pos = Pos( ls_cmd, "'", 2) ElseIf Left(ls_cmd, 1) = '"' Then // Find the end double quote ll_pos = Pos( ls_cmd, '"', 2) Else // Find the first blank ll_pos = Pos( ls_cmd, " " ) End If // If no blanks (only one argument), // set i to point to the hypothetical character // after the end of the string If ll_pos = 0 Then ll_pos = Len(ls_cmd) + 1 End If // Assign the arg to the argument array. // Number of chars copied is one less than the // position of the space found with Pos ls_argline = Left( ls_cmd, ll_pos - 1 ) // Remove the argument from the string // so the next argument becomes first If Left(ls_cmd, 1) = "'" Or Left(ls_cmd, 1) = '"' Then ls_cmd = Trim(Replace( ls_cmd, 1, ll_pos + 1, "" )) // Remove first quote ls_argline = Replace( ls_argline, 1, 1, "" ) Else ls_cmd = Trim(Replace( ls_cmd, 1, ll_pos, "" )) End If ls_arg[li_argcnt] = ls_argline Loop If li_argcnt = 0 Then ii_numerror = -3 Return -3 End If //Parse argument null lb_key = False li_argnew = 0 For li_arg = 1 To li_argcnt ls_prefixkey = Trim(Left(Trim(ls_arg[li_arg]), 1)) If ls_prefixkey = "-" Or ls_prefixkey = "--" Or ls_prefixkey = "/" Then If lb_key Then li_argnew ++ is_arg[li_argnew] = "" End If lb_key = True Else lb_key = False End If li_argnew ++ is_arg[li_argnew] = ls_arg[li_arg] Next If lb_key Then li_argnew ++ is_arg[li_argnew] = "" End If Return UpperBound(is_arg) |
Get Argument Value:
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 |
//==================================================================== // Function: n_parse_command.of_getvalue() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_key // reference string as_value //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2021/03/13 //-------------------------------------------------------------------- // Usage: n_parse_command.of_getvalue ( string as_key, ref string as_value ) //-------------------------------------------------------------------- // Modify History: // //==================================================================== Boolean lb_keyfound Int li_argcnt, li_arg String ls_value, ls_keyline If IsNull(as_key) Or Len(Trim(as_key)) = 0 Then ii_numerror = -2 Return -2 End If as_key = Trim(as_key) li_argcnt = UpperBound(is_arg) If li_argcnt <= 0 Then ii_numerror = -3 Return -3 End If For li_arg = 1 To li_argcnt Step 2 ls_keyline = is_arg[li_arg] If Upper(ls_keyline) = ( "-" + Upper( as_key)) Or Upper(ls_keyline) = ( "--" + Upper(as_key)) Or Upper(ls_keyline) = ( "/" + Upper(as_key)) Then If li_arg < li_argcnt Then ls_value = is_arg[li_arg + 1] lb_keyfound = True Exit End If End If Next If lb_keyfound Then as_value = ls_value ii_numerror = 1 Return 1 Else ii_numerror = -1 Return -1 End If |
Find Projects On Github click here
Good Luck!
Subscribe
Login
0 Comments
Oldest