Command line arguments in go

Get help with the installation and running of the Zeus IDE. Please do not post bug reports or feature requests here. When in doubt post your question here.
Post Reply
borboss
Posts: 2
Joined: Sat Oct 11, 2014 2:28 pm

Command line arguments in go

Post by borboss »

Hello!

I learning GO with ZEUS.
How do I pass command line arguments to the GO program, that I debug?
So I can access them via os.Args?

Thanks in advance!
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

How do I pass command line arguments to the GO program.
The way the default Zeus configuration compiles and runs Go executables is without arguments :(

For example consider this simple Go program:

Code: Select all

package main

import "fmt"

func main() {
    for i := 0; i < len(os.Args); i++ {
        fmt.Printf("Argument[%d] := %s\n", i, os.Args[i])
    }
}
If you build and run this Go file via the Zeus macro execute menu you get this output:

Code: Select all

Argument[0] := C:\DOCUME~1\JUSSIJ~1.XID\LOCALS~1\Temp\go-build498801551\command-line-arguments\_obj\exe\test.exe
So via the macro menu there is no option to pass in arguments :(

But if instead you create the following Zeus tool configuration (i.e. Options, Docment Types, Go doument type, Tools panel):

Code: Select all

Menu Text: Run '$fb.exe' with Arguments
Program Name: go.exe
Arguments: run $fn
(o) run Normal
[x] Capture Standard Output
[x] Capture Standard Error
[x] Ask for arguments
Now with the Go file active and using the newly configured tool menu, it will run the Go file but it will also prompt for user input.
that I debug?
I think you have identified a problem with the current Zeus debugger implmentation in that it is not possible to run the debugger while also passing in user defined arguments.

That issue will be fixed in the next Zeus release ;)

Cheers Jussi
borboss
Posts: 2
Joined: Sat Oct 11, 2014 2:28 pm

Thanks!

Post by borboss »

Thanks for such detailed answer!
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

Thanks for such detailed answer!
My pleasure.

I did some more thinking about this and unfortunately, the tool option above will not work if the Go program asks for user input:

For example, consider this Go program that does ask for input:

Code: Select all

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    for i := 0; i < len(os.Args); i++ {
        fmt.Printf("Argument[%d] := %s\n", i, os.Args[i])
    }

    fmt.Print("Enter some text: ")
    reader := bufio.NewReader(os.Stdin)
    text, _ := reader.ReadString('\n')
    fmt.Println(text)
}
If you run this via the tool it will not stop to ask for the user input, whereas if you run it via the macro it will.

The short answer for the difference in the two is the macro uses the tee.exe to duplicate the user input handle and this allows it to work.

So a better option would be to change the go_exec.py macro itself.

You can make that code change as follows:

1) With a Go file active go to the Macros panel and find the Execute macro command
2) Right click on the Execute command and select View Source Code option.
3) Replace that go_exec.py code with the code found below.
4) Right click on the Execute command and select the Add/Edit Templates option.
5) Edit the Execute macro and change the arguments from run to run with_args

Now when you run the Execute macro it will prompt for user input ;)

Cheers Jussi

Code: Select all

import os
import zeus

def key_macro():
   file_name = ""
   action = "run"
   arguments = "run"

   if zeus.argc() >= 1:
      # get the action passed in
      action = zeus.argv(0)

   if zeus.argc() == 2:
      # ask for arguments
      arguments = zeus.user_input("Agruments:", "").strip()
      if str(arguments) > 0:
         arguments = " " + arguments + " "

   if zeus.is_document() == 1:
      # see if this is a new file
      named = zeus.is_named()

      # save the current file
      result = zeus.FileSave()

      if (result == 0):
         if (named == 1):
            zeus.message("Unexpected error saving the file current document.")
            zeus.beep()
         return

      # use the currently active document
      file_name = zeus.macro_tag("$fn")

      # check that the file constains no whitespacw
      if (file_name.find(' ') <> -1):
         file_name = "\"" + file_name + "\""

   # if we have no document then ask the user
   if (len(file_name) == 0):
      # this is a run action
      action = "run"

      # the initial value
      nill = ""

      # ask the user to name the script that is to be run
      file_name = zeus.user_input("Go Script to Executed:", nill)

      if (len(file_name) == 0):
         zeus.message("The operation cancelled by user.")
         zeus.beep()
         return

   # the output file name
   file_output = file_name + ".ztow"

   # check that the file constains no whitespace
   if (file_output.find(' ') <> -1):
       # keep the quote marks
       file_output_ex = file_output
   else:
       # remove the file quotes to allow the file to be deleted
       file_output_ex = file_output.translate(None, '"')

   # remove any previous output file
   if os.access(file_output_ex, os.F_OK):
      os.remove(file_output_ex)

   # the Go 'run/build' the script using tee to capture the output to a file
   cmd = "Go.exe " + action + " " + file_name + arguments + " 2>&1 | tee " + file_output

   #zeus.message_box(0, cmd, "Test")

   # run the command as a MS-DOS command in a visible DOS window and wait for it to finish
   flags = 16+32

   if action == "run":
      # for the 'run' action use a visible window
      flags = flags + 64

   # build up the caption
   caption = "Go " + str.capitalize(action) + ": " + file_name

   # run the command which should be displayed in a DOS window
   result = zeus.system(cmd, "", flags, caption)

   # make sure the cmd.exe command completed successfully
   if result == 0:
      if action == "build" or action == "fix" or action == "vet":
         statinfo = os.stat(file_output)
         # check for any errors
         if statinfo.st_size == 0:
            zeus.message(str.capitalize(action) + " completed successfully.")
         else:
            # open the output file in a Zeus project window
            zeus.file_open_compiler(file_output, "Go.exe " + action + " " + file_name, file_name)
      else:
         # open the output file in a Zeus tool window
         zeus.file_open_tool(file_output, "Go.exe " + action + " " + file_name)
   else:
      # make sure the user will see the error
      if zeus.is_debug_enabled() == 0:
         zeus.debug_enable()

      # log the command to the debug window
      zeus.debug_output(cmd + "\n\n" + "Error executing the 'Go Format' command!\n")

      # set capture flags
      flags = 1+32+2+4

      # run the format again but this time capture the error output
      zeus.system(cmd, "", flags)

   # remove the output file
   if os.access(file_output_ex, os.F_OK):
      os.remove(file_output_ex)

key_macro() # run the macro
Post Reply