]> granicus.if.org Git - imagemagick/blobdiff - MagickWand/magick-cli.c
(no commit message)
[imagemagick] / MagickWand / magick-cli.c
index a497fd192aa8af3797476f7a9a24a341a51b14e7..955fbd21d719234b7da3ce7368b52d96ec62861d 100644 (file)
@@ -22,7 +22,7 @@
 %                               January 2012                                  %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
 #include "MagickCore/version.h"
 \f
 /* verbose debugging,
-      3 - option type details
-      9 - output options/artifacts/propertys
+      0 - no debug lines
+      3 - show option details  (better to use -debug Command now)
+      5 - image counts (after option runs)
 */
-#define MagickCommandDebug 3
-
-
-#if MagickCommandDebug >= 9
-/*
-  Temporary Debugging Information
-  FUTURE: these should be able to be printed out using 'percent escapes'
-  Actually 'Properities' can already be output with  "%[*]"
-*/
-static void OutputOptions(ImageInfo *image_info)
-{
-  const char
-    *option,
-    *value;
-
-  (void) FormatLocaleFile(stdout,"  Global Options:\n");
-  ResetImageOptionIterator(image_info);
-  while ((option=GetNextImageOption(image_info)) != (const char *) NULL ) {
-    (void) FormatLocaleFile(stdout,"    %s: ",option);
-    value=GetImageOption(image_info,option);
-    if (value != (const char *) NULL)
-      (void) FormatLocaleFile(stdout,"%s\n",value);
-  }
-  ResetImageOptionIterator(image_info);
-}
-
-static void OutputArtifacts(Image *image)
-{
-  const char
-    *artifact,
-    *value;
-
-  (void) FormatLocaleFile(stdout,"  Image Artifacts:\n");
-  ResetImageArtifactIterator(image);
-  while ((artifact=GetNextImageArtifact(image)) != (const char *) NULL ) {
-    (void) FormatLocaleFile(stdout,"    %s: ",artifact);
-    value=GetImageArtifact(image,artifact);
-    if (value != (const char *) NULL)
-      (void) FormatLocaleFile(stdout,"%s\n",value);
-  }
-  ResetImageArtifactIterator(image);
-}
-
-static void OutputProperties(Image *image,ExceptionInfo *exception)
-{
-  const char
-    *property,
-    *value;
-
-  (void) FormatLocaleFile(stdout,"  Image Properity:\n");
-  ResetImagePropertyIterator(image);
-  while ((property=GetNextImageProperty(image)) != (const char *) NULL ) {
-    (void) FormatLocaleFile(stdout,"    %s: ",property);
-    value=GetImageProperty(image,property,exception);
-    if (value != (const char *) NULL)
-      (void) FormatLocaleFile(stdout,"%s\n",value);
-  }
-  ResetImagePropertyIterator(image);
-}
-#endif
+#define MagickCommandDebug 0
 
 \f
 /*
@@ -150,22 +92,24 @@ static void OutputProperties(Image *image,ExceptionInfo *exception)
 %
 %  The format of the ProcessScriptOptions method is:
 %
-%    void ProcessScriptOptions(MagickCLI *cli_wand,int argc,char **argv,
-%               int index)
+%    void ProcessScriptOptions(MagickCLI *cli_wand,const char *filename,
+%       int argc,char **argv,int index)
 %
 %  A description of each parameter follows:
 %
 %    o cli_wand: the main CLI Wand to use.
 %
-%    o argc: the number of elements in the argument vector.
+%    o filename: the filename of script to process
 %
-%    o argv: A text array containing the command line arguments.
+%    o argc: the number of elements in the argument vector. (optional)
 %
-%    o index: offset for argc to CLI argumnet count
+%    o argv: A text array containing the command line arguments. (optional)
+%
+%    o index: offset of next argment in argv (script arguments) (optional)
 %
 */
-WandExport void ProcessScriptOptions(MagickCLI *cli_wand,int argc,char **argv,
-     int index)
+WandExport void ProcessScriptOptions(MagickCLI *cli_wand,const char *filename,
+     int argc,char **argv,int index)
 {
   ScriptTokenInfo
     *token_info;
@@ -181,29 +125,27 @@ WandExport void ProcessScriptOptions(MagickCLI *cli_wand,int argc,char **argv,
     *arg1,
     *arg2;
 
-  assert(argc>index); /* at least one argument - script name */
-  assert(argv != (char **)NULL);
-  assert(argv[index] != (char *)NULL);
-  assert(argv[argc-1] != (char *)NULL);
+  assert(filename != (char *)NULL ); /* at least one argument - script name */
   assert(cli_wand != (MagickCLI *) NULL);
   assert(cli_wand->signature == WandSignature);
-  if (cli_wand->wand.debug != MagickFalse)
-    (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",cli_wand->wand.name);
+  if (IfMagickTrue(cli_wand->wand.debug))
+    (void) LogMagickEvent(CommandEvent,GetMagickModule(),
+         "Processing script \"%s\"", filename);
 
   /* open file script or stream, and set up tokenizer */
-  token_info = AcquireScriptTokenInfo(argv[index]);
+  token_info = AcquireScriptTokenInfo(filename);
   if (token_info == (ScriptTokenInfo *) NULL) {
-    CLIWandExceptionFile(OptionFatalError,"UnableToOpenScript",argv[index]);
+    CLIWandExceptionFile(OptionFatalError,"UnableToOpenScript",filename);
     return;
   }
 
   /* define the error location string for use in exceptions
      order of localtion format escapes: filename, line, column */
   cli_wand->location="in \"%s\" at line %u,column %u";
-  if ( LocaleCompare("-", argv[index]) == 0 )
+  if ( LocaleCompare("-", filename) == 0 )
     cli_wand->filename="stdin";
   else
-    cli_wand->filename=argv[index];
+    cli_wand->filename=filename;
 
   /* Process Options from Script */
   option = arg1 = arg2 = (char*)NULL;
@@ -216,104 +158,106 @@ WandExport void ProcessScriptOptions(MagickCLI *cli_wand,int argc,char **argv,
         break; /* error or end of options */
     }
 
-    /* save option details */
-    CloneString(&option,token_info->token);
+    do { /* use break to loop to exception handler and loop */
+
+      /* save option details */
+      CloneString(&option,token_info->token);
 
-    /* get option, its argument count, and option type */
-    cli_wand->command = GetCommandOptionInfo(option);
-    count=cli_wand->command->type;
-    option_type=(CommandOptionFlags) cli_wand->command->flags;
+      /* get option, its argument count, and option type */
+      cli_wand->command = GetCommandOptionInfo(option);
+      count=cli_wand->command->type;
+      option_type=(CommandOptionFlags) cli_wand->command->flags;
 #if 0
-    (void) FormatLocaleFile(stderr, "Script: %u,%u: \"%s\" matched \"%s\"\n",
+      (void) FormatLocaleFile(stderr, "Script: %u,%u: \"%s\" matched \"%s\"\n",
           cli_wand->line, cli_wand->line, option, cli_wand->command->mnemonic );
 #endif
 
-    /* handle a undefined option - image read? */
-    if ( option_type == UndefinedOptionFlag ||
-         (option_type & NonMagickOptionFlag) != 0 ) {
+      /* handle a undefined option - image read - always for "magick-script" */
+      if ( option_type == UndefinedOptionFlag ||
+           (option_type & NonMagickOptionFlag) != 0 ) {
 #if MagickCommandDebug >= 3
-      (void) FormatLocaleFile(stderr, "Script %u,%u Non-Option: \"%s\"\n",
-                  cli_wand->line, cli_wand->line, option);
+        (void) FormatLocaleFile(stderr, "Script %u,%u Non-Option: \"%s\"\n",
+                    cli_wand->line, cli_wand->line, option);
 #endif
-      if ( IfMagickFalse(IsCommandOption(option))) {
-        /* non-option -- treat as a image read */
-        cli_wand->command=(const OptionInfo *)NULL;
-        CLIOption(cli_wand,"-read",option);
-        goto next_token;
+        if ( IfMagickFalse(IsCommandOption(option))) {
+          /* non-option -- treat as a image read */
+          cli_wand->command=(const OptionInfo *)NULL;
+          CLIOption(cli_wand,"-read",option);
+          break; /* next option */
+        }
+        CLIWandException(OptionFatalError,"UnrecognizedOption",option);
+        break; /* next option */
       }
-      CLIWandExceptionBreak(OptionFatalError,"UnrecognizedOption",option);
-      goto next_token;
-    }
 
-    if ( count >= 1 ) {
-      if( IfMagickFalse(GetScriptToken(token_info)) )
-        CLIWandException(OptionFatalError,"MissingArgument",option);
-      CloneString(&arg1,token_info->token);
-    }
-    else
-      CloneString(&arg1,(char *)NULL);
-
-    if ( count >= 2 ) {
-      if( IfMagickFalse(GetScriptToken(token_info)) )
-        CLIWandExceptionBreak(OptionFatalError,"MissingArgument",option);
-      CloneString(&arg2,token_info->token);
-    }
-    else
-      CloneString(&arg2,(char *)NULL);
+      if ( count >= 1 ) {
+        if( IfMagickFalse(GetScriptToken(token_info)) )
+          CLIWandException(OptionFatalError,"MissingArgument",option);
+        CloneString(&arg1,token_info->token);
+      }
+      else
+        CloneString(&arg1,(char *)NULL);
 
+      if ( count >= 2 ) {
+        if( IfMagickFalse(GetScriptToken(token_info)) )
+          CLIWandExceptionBreak(OptionFatalError,"MissingArgument",option);
+        CloneString(&arg2,token_info->token);
+      }
+      else
+        CloneString(&arg2,(char *)NULL);
 
-    /*
-      Process Options
-    */
+      /*
+        Process Options
+      */
 #if MagickCommandDebug >= 3
-    (void) FormatLocaleFile(stderr,
-      "Script %u,%u Option: \"%s\"  Count: %d  Flags: %04x  Args: \"%s\" \"%s\"\n",
-          cli_wand->line,cli_wand->line,option,count,option_type,arg1,arg2);
+      (void) FormatLocaleFile(stderr,
+        "Script %u,%u Option: \"%s\"  Count: %d  Flags: %04x  Args: \"%s\" \"%s\"\n",
+            cli_wand->line,cli_wand->line,option,count,option_type,arg1,arg2);
 #endif
-    /* Hard Depreciated Options, no code to execute - error */
-    if ( (option_type & DeprecateOptionFlag) != 0 ) {
-      CLIWandException(OptionError,"DeprecatedOptionNoCode",option);
-      if ( IfMagickTrue(CLICatchException(cli_wand, MagickFalse)) )
-        break;
-      goto next_token;
-    }
-
-    /* MagickCommandGenesis() options have no place in a magick script */
-    if ( (option_type & GenesisOptionFlag) != 0 ) {
-      CLIWandExceptionBreak(OptionError,"InvalidUseOfOption",option);
-      goto next_token;
-    }
+      /* Hard Depreciated Options, no code to execute - error */
+      if ( (option_type & DeprecateOptionFlag) != 0 ) {
+        CLIWandException(OptionError,"DeprecatedOptionNoCode",option);
+        break; /* next option */
+      }
 
-    if ( (option_type & SpecialOptionFlag) != 0 ) {
-      if ( LocaleCompare(option,"-exit") == 0 ) {
-        break; /* forced end of script */
+      /* MagickCommandGenesis() options have no place in a magick script */
+      if ( (option_type & GenesisOptionFlag) != 0 ) {
+        CLIWandException(OptionError,"InvalidUseOfOption",option);
+        break; /* next option */
       }
-      if ( LocaleCompare(option,"-script") == 0 ) {
-        /* FUTURE: call new script from this script */
-        CLIWandExceptionBreak(OptionError,"InvalidUseOfOption",option);
-        goto next_token;
+
+      /* handle any special 'script' options */
+      if ( (option_type & SpecialOptionFlag) != 0 ) {
+        if ( LocaleCompare(option,"-exit") == 0 ) {
+          goto loop_exit; /* break out of loop - return from script */
+        }
+        if ( LocaleCompare(option,"-script") == 0 ) {
+          /* FUTURE: call new script from this script - error for now */
+          CLIWandException(OptionError,"InvalidUseOfOption",option);
+          break; /* next option */
+        }
+        /* FUTURE: handle special script-argument options here */
+        /* handle any other special operators now */
+        CLIWandException(OptionError,"InvalidUseOfOption",option);
+        break; /* next option */
       }
-      /* FUTURE: handle special script-argument options here */
-      /* handle any other special operators now */
-      CLIWandExceptionBreak(OptionError,"InvalidUseOfOption",option);
-      goto next_token;
-    }
 
-    /* Process non-specific Option */
-    CLIOption(cli_wand, option, arg1, arg2);
+      /* Process non-specific Option */
+      CLIOption(cli_wand, option, arg1, arg2);
 
-next_token:
-#if MagickCommandDebug >= 9
-    OutputOptions(cli_wand->wand.image_info);
-    if ( cli_wand->wand.images != (Image *)NULL ) {
-      OutputArtifacts(cli_wand->wand.images);
-      OutputProperties(cli_wand->wand.images,cli_wand->wand.exception);
-    }
+    } while (0); /* break block to next option */
+
+#if MagickCommandDebug >= 5
+    fprintf(stderr, "Script Image Count = %ld\n",
+         GetImageListLength(cli_wand->wand.images) );
 #endif
-    if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse )
-      break;
+    if ( IfMagickTrue(CLICatchException(cli_wand, MagickFalse)) )
+      break;  /* exit loop */
   }
 
+  /*
+     Loop exit - check for some tokenization error
+  */
+loop_exit:
 #if MagickCommandDebug >= 3
   (void) FormatLocaleFile(stderr, "Script End: %d\n", token_info->status);
 #endif
@@ -343,6 +287,9 @@ next_token:
       CLIWandException(OptionFatalError,"ScriptIsBinary","");
       break;
   }
+  if (IfMagickTrue(cli_wand->wand.debug))
+    (void) LogMagickEvent(CommandEvent,GetMagickModule(),
+         "Script End \"%s\"", filename);
 
   /* Clean up */
   token_info = DestroyScriptTokenInfo(token_info);
@@ -366,8 +313,8 @@ next_token:
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 %  ProcessCommandOptions() reads and processes arguments in the given
-%  command line argument array. The array does not contain the command
-%  being processed, only the options.
+%  command line argument array. The 'index' defines where in the array we
+%  should begin processing
 %
 %  The 'process_flags' can be used to control and limit option processing.
 %  For example, to only process one option, or how unknown and special options
@@ -376,8 +323,8 @@ next_token:
 %
 %  The format of the ProcessCommandOptions method is:
 %
-%    int ProcessCommandOptions(MagickCLI *cli_wand,int argc,char **argv,
-%           int index, ProcessOptionFlags process_flags )
+%    int ProcessCommandOptions(MagickCLI *cli_wand,
+%           int argc,char **argv,int index)
 %
 %  A description of each parameter follows:
 %
@@ -419,16 +366,19 @@ WandExport int ProcessCommandOptions(MagickCLI *cli_wand, int argc,
   assert(argv[argc-1] != (char *)NULL);
   assert(cli_wand != (MagickCLI *) NULL);
   assert(cli_wand->signature == WandSignature);
-  if (cli_wand->wand.debug != MagickFalse)
-    (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",cli_wand->wand.name);
 
   /* define the error location string for use in exceptions
      order of localtion format escapes: filename, line, column */
-  cli_wand->location="at %s argument %u";
+  cli_wand->location="at %s arg %u";
   cli_wand->filename="CLI";
+  cli_wand->line=index;  /* note first argument we will process */
+
+  if (IfMagickTrue(cli_wand->wand.debug))
+    (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(),
+         "- Starting (\"%s\")", argv[index]);
 
   end = argc;
-  if ( (cli_wand->process_flags & ProcessImpliedWrite) != 0 )
+  if ( (cli_wand->process_flags & ProcessImplictWrite) != 0 )
     end--; /* the last arument is an implied write, do not process directly */
 
   for (i=index; i < end; i += count +1) {
@@ -436,95 +386,97 @@ WandExport int ProcessCommandOptions(MagickCLI *cli_wand, int argc,
     if ( (cli_wand->process_flags & ProcessOneOptionOnly) != 0 && i != index )
       return(i);
 
-    option=argv[i];
-    cli_wand->line=i;  /* note the argument for this option */
+    do { /* use break to loop to exception handler and loop */
+
+      option=argv[i];
+      cli_wand->line=i;  /* note the argument for this option */
 
-    /* get option, its argument count, and option type */
-    cli_wand->command = GetCommandOptionInfo(argv[i]);
-    count=cli_wand->command->type;
-    option_type=(CommandOptionFlags) cli_wand->command->flags;
+      /* get option, its argument count, and option type */
+      cli_wand->command = GetCommandOptionInfo(argv[i]);
+      count=cli_wand->command->type;
+      option_type=(CommandOptionFlags) cli_wand->command->flags;
 #if 0
-    (void) FormatLocaleFile(stderr, "CLI %d: \"%s\" matched \"%s\"\n",
-          i, argv[i], cli_wand->command->mnemonic );
+      (void) FormatLocaleFile(stderr, "CLI %d: \"%s\" matched \"%s\"\n",
+            i, argv[i], cli_wand->command->mnemonic );
 #endif
 
-    if ( option_type == UndefinedOptionFlag ||
-         (option_type & NonMagickOptionFlag) != 0 ) {
+      if ( option_type == UndefinedOptionFlag ||
+           (option_type & NonMagickOptionFlag) != 0 ) {
 #if MagickCommandDebug >= 3
-      (void) FormatLocaleFile(stderr, "CLI %d Non-Option: \"%s\"\n", i, option);
+        (void) FormatLocaleFile(stderr, "CLI arg %d Non-Option: \"%s\"\n",
+             i, option);
 #endif
-      if ( IfMagickFalse(IsCommandOption(option)) ) {
-        if ( (cli_wand->process_flags & ProcessNonOptionImageRead) != 0 ) {
-          /* non-option -- treat as a image read */
-          cli_wand->command=(const OptionInfo *)NULL;
-          CLIOption(cli_wand,"-read",option);
-          goto next_argument;
+        if ( IfMagickFalse(IsCommandOption(option)) ) {
+          if ( (cli_wand->process_flags & ProcessImplictRead) != 0 ) {
+            /* non-option -- treat as a image read */
+            cli_wand->command=(const OptionInfo *)NULL;
+            CLIOption(cli_wand,"-read",option);
+            break; /* next option */
+          }
         }
+        CLIWandException(OptionFatalError,"UnrecognizedOption",option);
+        break; /* next option */
       }
-      CLIWandException(OptionFatalError,"UnrecognizedOption",option);
-      goto next_argument;
-    }
 
-    if ( ((option_type & SpecialOptionFlag) != 0 ) &&
-         ((cli_wand->process_flags & ProcessScriptOption) != 0) &&
-         (LocaleCompare(option,"-script") == 0) ) {
-      /* Call Script from CLI, with a filename as a zeroth argument.
-         NOTE: -script may need to use the 'implict write filename' argument
-         so it must be handled specially to prevent a 'missing argument' error.
-      */
-      if ( (i+count) >= argc )
-        CLIWandException(OptionFatalError,"MissingArgument",option);
-      ProcessScriptOptions(cli_wand,argc,argv,i+1);
-      return(argc);  /* Script does not return to CLI -- Yet */
-                     /* FUTURE: when it does, their may be no write arg! */
-    }
+      if ( ((option_type & SpecialOptionFlag) != 0 ) &&
+           ((cli_wand->process_flags & ProcessScriptOption) != 0) &&
+           (LocaleCompare(option,"-script") == 0) ) {
+        /* Call Script from CLI, with a filename as a zeroth argument.
+           NOTE: -script may need to use the 'implict write filename' argument
+           so it must be handled specially to prevent a 'missing argument' error.
+        */
+        if ( (i+count) >= argc )
+          CLIWandException(OptionFatalError,"MissingArgument",option);
+        ProcessScriptOptions(cli_wand,argv[i+1],argc,argv,i+count);
+        return(argc);  /* Script does not return to CLI -- Yet */
+                       /* FUTURE: when it does, their may be no write arg! */
+      }
 
-    if ((i+count) >= end ) {
-      CLIWandException(OptionFatalError,"MissingArgument",option);
-      if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse )
-        return(end);
-      goto next_argument; /* no more arguments unable to proceed */
-    }
+      if ((i+count) >= end ) {
+        CLIWandException(OptionFatalError,"MissingArgument",option);
+        if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse )
+          return(end);
+        break; /* next option - not that their is any! */
+      }
 
-    arg1 = ( count >= 1 ) ? argv[i+1] : (char *)NULL;
-    arg2 = ( count >= 2 ) ? argv[i+2] : (char *)NULL;
+      arg1 = ( count >= 1 ) ? argv[i+1] : (char *)NULL;
+      arg2 = ( count >= 2 ) ? argv[i+2] : (char *)NULL;
 
-    /*
-      Process Known Options
-    */
+      /*
+        Process Known Options
+      */
 #if MagickCommandDebug >= 3
-    (void) FormatLocaleFile(stderr,
-      "CLI %u Option: \"%s\"  Count: %d  Flags: %04x  Args: \"%s\" \"%s\"\n",
-          i,option,count,option_type,arg1,arg2);
+      (void) FormatLocaleFile(stderr,
+        "CLI arg %u Option: \"%s\"  Count: %d  Flags: %04x  Args: \"%s\" \"%s\"\n",
+            i,option,count,option_type,arg1,arg2);
 #endif
+      /* ignore 'genesis options' in command line args */
+      if ( (option_type & GenesisOptionFlag) != 0 )
+        break; /* next option */
+
+      /* Handle any special options for CLI (-script handled above) */
+      if ( (option_type & SpecialOptionFlag) != 0 ) {
+        if ( (cli_wand->process_flags & ProcessExitOption) != 0
+             && LocaleCompare(option,"-exit") == 0 )
+          return(i+count);
+        break; /* next option */
+      }
 
-    if ( (option_type & GenesisOptionFlag) != 0 )
-      goto next_argument; /* ignored this from command line args */
-
-    if ( (option_type & SpecialOptionFlag) != 0 ) {
-      if ( (cli_wand->process_flags & ProcessExitOption) != 0
-           && LocaleCompare(option,"-exit") == 0 )
-        return(i+count);
-      goto next_argument;
-    }
+      /* Process standard image option */
+      CLIOption(cli_wand, option, arg1, arg2);
 
-    /* Process a Normal Option */
-    CLIOption(cli_wand, option, arg1, arg2);
+    } while (0); /* break block to next option */
 
-next_argument:
-#if MagickCommandDebug >= 9
-    OutputOptions(cli_wand->wand.image_info);
-    if ( cli_wand->wand.images != (Image *)NULL ) {
-      OutputArtifacts(cli_wand->wand.images);
-      OutputProperties(cli_wand->wand.images,cli_wand->wand.exception);
-    }
+#if MagickCommandDebug >= 5
+    (void) FormatLocaleFile(stderr, "CLI-post Image Count = %ld\n",
+         (long) GetImageListLength(cli_wand->wand.images) );
 #endif
     if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse )
       return(i+count);
   }
   assert(i==end);
 
-  if ( (cli_wand->process_flags & ProcessImpliedWrite) == 0 )
+  if ( (cli_wand->process_flags & ProcessImplictWrite) == 0 )
     return(end); /* no implied write -- just return to caller */
 
   assert(end==argc-1); /* end should not include last argument */
@@ -535,19 +487,19 @@ next_argument:
   option=argv[i];
   cli_wand->line=i;
 
-#if MagickCommandDebug >= 3
-  (void) FormatLocaleFile(stderr, "CLI %d Write File: \"%s\"\n", i, option );
-#endif
-
-  /* check that stacks are empty */
+  /* check that stacks are empty - or cause exception */
   if (cli_wand->image_list_stack != (Stack *)NULL)
-    CLIWandException(OptionError,"UnbalancedParenthesis", "(eof)");
+    CLIWandException(OptionError,"UnbalancedParenthesis", "(end of cli)");
   else if (cli_wand->image_info_stack != (Stack *)NULL)
-    CLIWandException(OptionError,"UnbalancedBraces", "(eof)");
+    CLIWandException(OptionError,"UnbalancedBraces", "(end of cli)");
   if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse )
     return(argc);
 
-  /* This is a valid 'do no write' option - no images needed */
+#if MagickCommandDebug >= 3
+  (void) FormatLocaleFile(stderr,"CLI arg %d Write File: \"%s\"\n",i,option);
+#endif
+
+  /* Valid 'do no write' replacement option (instead of "null:") */
   if (LocaleCompare(option,"-exit") == 0 )
     return(argc);  /* just exit, no image write */
 
@@ -585,20 +537,20 @@ next_argument:
 %
 %  The format of the MagickImageCommand method is:
 %
-%      MagickBooleanType MagickImageCommand(ImageInfo *image_info,
-%           int argc, char **argv, char **metadata, ExceptionInfo *exception)
+%      MagickBooleanType MagickImageCommand(ImageInfo *image_info,int argc,
+%        char **argv,char **metadata,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
 %    o image_info: the starting image_info structure
-%         (for compatibilty with MagickCommandGenisis())
+%      (for compatibilty with MagickCommandGenisis())
 %
 %    o argc: the number of elements in the argument vector.
 %
 %    o argv: A text array containing the command line arguments.
 %
 %    o metadata: any metadata (for VBS) is returned here.
-%         (for compatibilty with MagickCommandGenisis())
+%      (for compatibilty with MagickCommandGenisis())
 %
 %    o exception: return any errors or warnings in this structure.
 %
@@ -618,26 +570,26 @@ static void MagickUsage(MagickBooleanType verbose)
   if (len>=7 && LocaleCompare("convert",name+len-7) == 0) {
     /* convert usage */
     (void) FormatLocaleFile(stdout,
-       "Usage: %s [{option}|{image}...] {output_image}\n",name);
+       "Usage: %s [ {option} | {image} ... ] {output_image}\n",name);
     (void) FormatLocaleFile(stdout,
-       "       %s -help|-version|-usage|-list {option}\n\n",name);
+       "       %s -help | -version | -usage | -list {option}\n\n",name);
     return;
   }
   else if (len>=6 && LocaleCompare("script",name+len-6) == 0) {
     /* magick-script usage */
     (void) FormatLocaleFile(stdout,
-       "Usage: %s {filename} [{script_args}...]\n",name);
+      "Usage: %s {filename} [ {script_args} ... ]\n",name);
   }
   else {
     /* magick usage */
     (void) FormatLocaleFile(stdout,
-       "Usage: %s [{option}|{image}...] {output_image}\n",name);
+       "Usage: %s [ {option} | {image} ... ] {output_image}\n",name);
     (void) FormatLocaleFile(stdout,
-       "       %s [{option}|{image}...] -script {filename} [{script_args}...]\n",
+       "       %s [ {option} | {image} ... ] -script {filename} [ {script_args} ...]\n",
        name);
   }
   (void) FormatLocaleFile(stdout,
-       "       %s -help|-version|-usage|-list {option}\n\n",name);
+    "       %s -help | -version | -usage | -list {option}\n\n",name);
 
   if (IfMagickFalse(verbose))
     return;
@@ -722,13 +674,22 @@ WandExport MagickBooleanType MagickImageCommand(ImageInfo *image_info,
   size_t
     len;
 
+  assert(image_info != (ImageInfo *)NULL);
+
   /* For specific OS command line requirements */
   ReadCommandlLine(argc,&argv);
 
   /* Initialize special "CLI Wand" to hold images and settings (empty) */
   cli_wand=AcquireMagickCLI(image_info,exception);
+  cli_wand->location="Initializing";
+  cli_wand->filename=argv[0];
   cli_wand->line=1;
 
+  if (IfMagickTrue(cli_wand->wand.debug))
+    (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(),
+         "\"%s\"",argv[0]);
+
+
   GetPathComponent(argv[0],TailPath,cli_wand->wand.name);
   SetClientName(cli_wand->wand.name);
   ConcatenateMagickString(cli_wand->wand.name,"-CLI",MaxTextExtent);
@@ -738,31 +699,38 @@ WandExport MagickBooleanType MagickImageCommand(ImageInfo *image_info,
   /* "convert" command - give a "depreciation" warning" */
   if (len>=7 && LocaleCompare("convert",argv[0]+len-7) == 0) {
     cli_wand->process_flags = ConvertCommandOptionFlags;
-    /*(void) FormatLocaleFile(stderr,"WARNING: %s\n",
-             "The convert is depreciated in IMv7, use \"magick\"\n");*/
+    (void) FormatLocaleFile(stderr,"WARNING: %s\n",
+             "The convert is depreciated in IMv7, use \"magick\"\n");
   }
 
   /* Special Case:  If command name ends with "script" implied "-script" */
   if (len>=6 && LocaleCompare("script",argv[0]+len-6) == 0) {
     if (argc >= 2 && (  (*(argv[1]) != '-') || (strlen(argv[1]) == 1) )) {
       GetPathComponent(argv[1],TailPath,cli_wand->wand.name);
-      ProcessScriptOptions(cli_wand,argc,argv,1);
+      ProcessScriptOptions(cli_wand,argv[1],argc,argv,2);
       goto Magick_Command_Cleanup;
     }
   }
 
   /* Special Case: Version Information and Abort */
   if (argc == 2) {
-    if (LocaleCompare("-version",argv[1]) == 0) { /* just version */
+    if ((LocaleCompare("-version",argv[1]) == 0)   || /* GNU standard option */
+        (LocaleCompare("--version",argv[1]) == 0) ) { /* just version */
       CLIOption(cli_wand, "-version");
       goto Magick_Command_Exit;
     }
     if ((LocaleCompare("-help",argv[1]) == 0)   || /* GNU standard option */
         (LocaleCompare("--help",argv[1]) == 0) ) { /* just a brief summary */
+      if (IfMagickTrue(cli_wand->wand.debug))
+        (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(),
+            "- Special Option \"%s\"", argv[1]);
       MagickUsage(MagickFalse);
       goto Magick_Command_Exit;
     }
     if (LocaleCompare("-usage",argv[1]) == 0) {   /* both version & usage */
+      if (IfMagickTrue(cli_wand->wand.debug))
+        (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(),
+            "- Special Option \"%s\"", argv[1]);
       CLIOption(cli_wand, "-version" );
       MagickUsage(MagickTrue);
       goto Magick_Command_Exit;
@@ -779,6 +747,9 @@ WandExport MagickBooleanType MagickImageCommand(ImageInfo *image_info,
 
   /* Special "concatenate option (hidden) for delegate usage */
   if (LocaleCompare("-concatenate",argv[1]) == 0) {
+    if (IfMagickTrue(cli_wand->wand.debug))
+        (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(),
+            "- Special Option \"%s\"", argv[1]);
     ConcatenateImages(argc,argv,exception);
     goto Magick_Command_Exit;
   }
@@ -798,7 +769,7 @@ WandExport MagickBooleanType MagickImageCommand(ImageInfo *image_info,
        First argument in the argv array is the script name to read.
     */
     GetPathComponent(argv[2],TailPath,cli_wand->wand.name);
-    ProcessScriptOptions(cli_wand,argc,argv,2);
+    ProcessScriptOptions(cli_wand,argv[2],argc,argv,3);
   }
   else {
     /* Normal Command Line, assumes output file as last option */
@@ -807,6 +778,12 @@ WandExport MagickBooleanType MagickImageCommand(ImageInfo *image_info,
   /* ------------- */
 
 Magick_Command_Cleanup:
+  cli_wand->location="Cleanup";
+  cli_wand->filename=argv[0];
+  if (IfMagickTrue(cli_wand->wand.debug))
+    (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(),
+         "\"%s\"",argv[0]);
+
   /* recover original image_info and clean up stacks
      FUTURE: "-reset stacks" option  */
   while (cli_wand->image_list_stack != (Stack *)NULL)
@@ -829,10 +806,10 @@ Magick_Command_Cleanup:
     format="%w,%h,%m";   // Get this from image_info Option splaytree
 
     text=InterpretImageProperties(image_info,cli_wand->wand.images,format,
-         exception);
+      exception);
     if (text == (char *) NULL)
       ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
-           "MemoryAllocationFailed","'%s'", GetExceptionMessage(errno));
+        "MemoryAllocationFailed","`%s'", GetExceptionMessage(errno));
     else {
       (void) ConcatenateString(&(*metadata),text);
       text=DestroyString(text);
@@ -840,6 +817,12 @@ Magick_Command_Cleanup:
   }
 
 Magick_Command_Exit:
+  cli_wand->location="Exiting";
+  cli_wand->filename=argv[0];
+  if (IfMagickTrue(cli_wand->wand.debug))
+    (void) CLILogEvent(cli_wand,CommandEvent,GetMagickModule(),
+         "\"%s\"",argv[0]);
+
   /* Destroy the special CLI Wand */
   cli_wand->wand.image_info = (ImageInfo *)NULL; /* not these */
   cli_wand->wand.exception = (ExceptionInfo *)NULL;