Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
27 user(s) are online (22 user(s) are browsing Forums)

Members: 0
Guests: 27

more...

Support us!

Headlines

 
  Register To Post  

ReadArgs()
Just can't stay away
Just can't stay away


See User information
Can someone point me to some examples of using ReadArgs() ?
I am not sure how to test for the args.
Thanks

Go to top
Re: ReadArgs()
Home away from home
Home away from home


See User information
Here's how I do it in my AOrganiser program.

LONG ArgArray[] =
{
    (
LONG)"YES",            /* CX_POPUP */
    
(LONG)"ctrl alt d",     /* CX_POPKEY */
    
(LONG)0,                /* CX_PRIORITY */
    
0
};

/* process args with read args or read tooltypes */
/* return a pinter to a tooltype array for CX */

BOOL FromWB FALSE;
struct DiskObject *dob NULL;
static 
struct RDArgs *rdargs;

STRPTR template "CX_POPUP/K,CX_POPKEY/K,CX_PRIORITY/K/N,FILE";


static 
STRPTR *setupArgs(int argcSTRPTRargv)
{
    
STRPTR *ttarray NULL;
    if(
argc == 0)
    {
        
/* We were invoked from WB */
        
int i 0;
        
FromWB TRUE;
        
struct WBStartup *wbs = (struct WBStartup *)argv;
        if(
wbs->sm_NumArgs 1)
        {
            
1;
        }
        
BPTR oldcd IDOS->CurrentDir(wbs->sm_ArgList[i].wa_Lock);
        
dob=IIcon->GetDiskObject(wbs->sm_ArgList[i].wa_Name);
        if(
wbs->sm_NumArgs 1)
        {
            if((
Filename IExec->AllocVec(sizeof(BYTE) * 1024MEMF_ANY|MEMF_CLEAR)))
            {
                
IDOS->NameFromLock(wbs->sm_ArgList[i].wa_Lock,Filename,1024);
                
IDOS->AddPart(Filename,wbs->sm_ArgList[i].wa_Name,1024);
            }
        }
        if(
dob)
        {
            if(
IIcon->FindToolType(dob->do_ToolTypes"CX_POPUP"))
            {
                
ArgArray[0] = (LONG)IIcon->FindToolType(dob->do_ToolTypes"CX_POPUP");
            }
            
ArgArray[3] = (LONG)IIcon->FindToolType(dob->do_ToolTypes"FILE");
            
ttarray dob->do_ToolTypes;
        }
        else
        {
            if((
ttarray IExec->AllocVec(sizeof(UBYTE *)*4,MEMF_ANY|MEMF_CLEAR)))
            {
                 
ttarray[0] = NULL;
            }
        }
        
IDOS->CurrentDir(oldcd);
    }
    else
    {
        
/* Invoked from CLI use readargs */
        
rdargs IDOS->ReadArgs(template,ArgArray,NULL);
        
/* Create a "fake"  ttarray */
        
if((ttarray IExec->AllocVec(sizeof(UBYTE *)*4,MEMF_ANY|MEMF_CLEAR)))
        {
            
ULONG i=0;
            
ULONG length =0;

            
length strlen((char *)ArgArray[0]) + 10// "CX_POPUP=....\0"
            
if((ttarray[i] = IExec->AllocVec(length,MEMF_ANY)))
            {
                
sprintf(ttarray[i++],"CX_POPUP=%s",(UBYTE *)ArgArray[0]);
            }
            
length strlen((char *)ArgArray[1]) + 11// "CX_POPKEY=....\0"
            
if((ttarray[i] = IExec->AllocVec(length,MEMF_ANY)))
            {
                
sprintf(ttarray[i++],"CX_POPKEY=%s",(UBYTE *)ArgArray[1]);
            }
            
length 13// "CX_PRIORITY=....\0"
            
if((ttarray[i] = IExec->AllocVec(length,MEMF_ANY)))
            {
                
sprintf(ttarray[i++],"CX_PRIORITY=%d",(int)ArgArray[2]);
            }
            
ttarray[i] = NULL;



        }

    }
    return 
ttarray;
}

void releaseArgs(STRPTR *ttarray)
{
    if(
FromWB)
    {
        if(
dob)IIcon->FreeDiskObject(dob);
        if(
FilenameIExec->FreeVec(Filename);
    }
    else
    {

        
IDOS->FreeArgs(rdargs);
        if(
ttarray)
        {
            
ULONG i 0;
            while(
ttarray[i])
            {
                
IExec->FreeVec(ttarray[i++]);
            }
            
IExec->FreeVec(ttarray);
        }
    }
}


then

int main(int argcchar **argv)
{
        
STRPTR *ttypes;

        if(
openLibraries())
        {
            if((
CALENDAR_Class initCalendarClass()))
            {
                
ttypes setupArgs(argcargv);
  if(
ArgArray[3])
                {
                    
int error;
                    if((
error Loadfile(&Events,(STRPTR)ArgArray[3])) <0)
                    {
                        
// Reporterror(error);
                    
}
                    if(
Filename)
                    {
                        
IExec->FreeVec(Filename);
                    }
                    
Filename Dupstr((STRPTR)ArgArray[3],-1);
                }
                else if (
Filename)
                {
                    
int error;
                    if((
error Loadfile(&Events,Filename)) <0)
                    {
                        
// Reporterror(error);
                    
}

                }
                else
                {
                    
Filename Dupstr("Unnamed",-1);
                }

               
// do a whole heap of stuff here...

                
releaseArgs(ttypes);
                
freeCalendarClass(CALENDAR_Class);
            }
        }
        
closeLibraries();
        return(
0);
}


[edit]
Added some more code for clarity
[/edit]


Edited by broadblues on 2011/7/7 16:20:11
Go to top
Re: ReadArgs()
Not too shy to talk
Not too shy to talk


See User information
@broadblues:

your code for CX_PRIORITY does not work. /N returns a pointer to a long, not a long.


@JosDuchIt:

here is a smaller example:

#include <proto/dos.h>

int main (void)

{
struct RDArgs *rdargs;
struct {
    
char *file;
    
char *pubscreen;
    
long *maxdepth;
    
long recursive;
    
char **modes;
    } 
args = {0};

rdargs ReadArgs ("FILE/A,PUBSCREEN/K,MAXDEPTH/N,RECURSIVE/S,MODES/M",(APTR)&args,NULL);

/*

/A -> required
/K -> keyword must be given
/N -> numeric
/S -> switch
/M -> multiple values

combinations like /A/N or /K/N or /A/K/N are allowed.

*/



if (!rdargs)    /* input does not match the template */
    
{
    
PrintFault (IoErr(),NULL);
    return (
RETURN_ERROR);
    }

Printf ("your arguments:\n");

Printf ("  file = %s\n",args.file);

if (
args.pubscreen/* optional argument */
    
Printf ("  pubscreen = %s\n",args.pubscreen);
else
    
Printf ("  no pubscreen given\n");

if (
args.maxdepth/* optional numeric argument */
    
Printf ("  maxdepth = %ld\n",*args.maxdepth);
else
    
Printf ("  no maxdepth given\n");

if (
args.recursive/* boolean argument */
    
Printf ("  recursive = Yes\n");
else
    
Printf ("  recursive = No\n");

if (
args.modes && *args.modes)
    {
    
char **tt;
    
long i 0;
    for (
tt args.modes; *tttt++)
        
Printf ("  mode %ld = %s\n",++i,*tt);
    }
else
    
Printf ("  no modes given\n");

FreeArgs (rdargs);
return (
RETURN_OK);
}


Less code for more clarity

Go to top
Re: ReadArgs()
Just can't stay away
Just can't stay away


See User information
@broadblues, @thomas,
thanks a lot



Go to top
Re: ReadArgs()
Just can't stay away
Just can't stay away


See User information
These are extracts from the code used that i am trying to port to OS4.
The source compiles OK (but for a number of warnings).
The template does not work as expected eg with the sole arguments
myfile
i enter the 'delete' branch and not the 'view' branch
More: the behaviour is not always the same. I can't explain the error though

#define TEMPLATE   "FILE/A,VIEW/S,ADD/K,DEL/K/N"
#define NUM        4

#define ARG_NAME    args[0]  ///OK
#define ARG_VIEW    args[1]
#define ARG_ADD        args[2]
#define ARG_DEL        args[3]
int main(void) {
    
LONG    args[4]
        
rd ReadArgs(TEMPLATE, &argsNULL);
        if(
rd) {
            if (
ARG_NAME) {    // arg[0]
                
if(ARG_ADD) { ...
                else if(
ARG_DEL) {...
                else {  
/* else VIEW  */


Edit:
initialising args solves this behaviour it seems
args[0] = 0L;
args[1] = 0L;
args[3] = 0L;


Edited by JosDuchIt on 2011/7/7 19:35:14
Go to top
Re: ReadArgs()
Not too shy to talk
Not too shy to talk


See User information
Quote:
initialising args solves this behaviour it seems


Yes, you need to initialise the argument array with your defaults. Fields that do not appear in the command line remain unchanged.

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project