Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Program command line args and program return value?

177 views
Skip to first unread message

Steve Litt

unread,
Sep 11, 2010, 10:49:48 PM9/11/10
to harbou...@googlegroups.com
Hi all,

I've been beating myself crazy exploring command line arguments and return
values in Harbour. I'm used to this in C:

=====================================
#include <stdio.h>
int main(int argc, char *argv[]){
int i;
for (i=0; i < argc; i++)
printf("%s\n", argv[i]);
return(17);
}
=====================================

I wasn't so successful in Clipper. No matter what I put as the args to main, I
had to predeclare at least the maximum number of args expected, and later
convert that to an array. And for the life of me, I can't find a way to return
a non-zero such that Bash's $? variable shows anything but 0. Here's the code
I wrote:

===================================
function Main(arg1, arg2, arg3, arg4, arg5)
LOCAL arg
LOCAL argc := 0
LOCAL argv := {arg1, arg2, arg3, arg4, arg5}
? "Arguments follow ..."
FOR EACH arg IN argv
if arg = Nil
EXIT
ENDIF
argc++
? "Argument ", argc, "=", arg
NEXT
? "No more arguments."
? "There were ", argc, " arguments total."
RETURN 17 //Doesnt work, always 0
===================================

Here's the result:

=====================================
slitt@mydesk:~/hb_ex$ hbrun args.prg one two three

Arguments follow ...
Argument 1 = one
Argument 2 = two
Argument 3 = three
No more arguments.
There were 3 arguments total.
slitt@mydesk:~/hb_ex$ echo $?
0
slitt@mydesk:~/hb_ex$
=====================================

Kludge city! There's got to be a better way, and the return statement doesn't
influence the executable's $?. What am I missing?

Thanks

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt

do...@people.net.au

unread,
Sep 12, 2010, 1:12:40 AM9/12/10
to harbou...@googlegroups.com
Hi Steve

Its actually easy but you have hit on Harbour's one real shortcoming - lack of documentation.  I have actually made a small start on documenting the API and making a better version of xhb-diff.txt which explains the differences between Harbour and xHarbour (the two forks).

Back to your question

simply declare as follows:

PROCEDURE Main(...)

or presumably FUNCTION Main(...).  I don't actually know why I used PROCEDURE in the pices of code I looked at.

Then to access them in the manner you are talking about you can do something like:

  arr_Parameters := HB_AParams()
  int_ParamCount := LEN( arr_Parameters )

  IF int_ParamCount > 0
    FOR int_LoopCounter = 1 TO int_ParamCount
      CheckParameter( arr_Parameters[int_LoopCounter] )
    NEXT
  ENDIF

Actually this is documented - but not where you might expect it but in xhb-diff.txt (which should have come along with the Harbour source).

I will try to attach my improved (hopefully) but not yet finished pdf version.

Will send more re GTK+ interface

Hope above helps

Regards
Doug





On Sat 11/09/10 22:49 , Steve Litt sl...@troubleshooters.com sent:
Hi all,

I've been beating myself crazy exploring command line arguments and return
values in Harbour. I'm used to this in C:

=====================================
#include
http://www.recession-relief.US">http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt">http://www.twitter.com/stevelitt


xhb-diff2.pdf
Reply all
Reply to author
Forward
0 new messages