Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will be making a program that can take command like args. Using command line

ID: 3805744 • Letter: Y

Question

You will be making a program that can take command like args.

Using command line arguments, allow the program to do the following

If the command is -i, print: Integer, and then print the integer after it.

If the command is -f print: float, and then print the float after it

if the command is -s print: string, and then print the string after it.

If the command is -h print: all the commands, and the syntax

If the command is something not listed, do the same as the -h command.

It doesn't matter the number or the order -- do all the commands that the person typed.

extra credit:

Is someone does a command, but it is not the right syntax -- show the correct syntax.

Example entries into the program:

prog.exe -i 3

prog.exe -f 3.14 -i 6

prog.exe -h

Explanation / Answer

The basic syntax for a Tcl command is:

command arg1 arg2 arg3 ...

Commands are terminated by either a newline character (not usually displayed) or a semicolon, ";".

Spaces are key characters which separate arguments.

Commands can only be continued to the next line with a "" at the end of the line, or with the beginning of an argument grouping using “{” or ".

Argument Grouping

If a space is to occur inside an argument, then the argument must be grouped as one so that it is not interpreted as separate arguments. For example, the syntax for the puts command is:

puts ?-nonewline? ?channelId? string

The text between question marks, "?...?" comprises optional arguments or switches which may or may not be present. Consider the following commands and their return values:

%puts {Hello world}
Hello world

%puts Hello world
cannot find channel named "Hello"

In the first example, note that the grouping brackets, "{...}", were stripped out by the interpreter, but it was treated as a single argument.

In the second example, the arguments, "Hello" and "world" are treated as separate arguments where "Hello" is the channelId. At this point, no channel named "Hello" exists.

There are 3 types of grouping:

{...} - Groups without allowing substitution inside

"..." - Groups while allowing substitution inside.

[...] - Interrupts the current command to do the command contained inside the brackets. The next word following “[” must be a command. This creates a nested Tcl command structure. Some Tcl documentations do not consider this a grouping structure.

Note:  The puts command will not output text to the ANSYS ICEM CFD message window. To do this, use mess or ic_mess. This command automatically does the -nonewline switch. You must use “ ” inside the message string to insert a new line.

Substitution

There are 3 types of substitution:

Command - This is done whenever there are square brackets, “[...]”. The command is executed and the return value is substituted before continuing with the original command.

Variable - Variable substitutions are done by using a dollar sign, “$”, in front of the variable.

Backslash - When a backslash, “” is encountered, the next character is treated special. For example, “ ” means to substitute a new line character.

The following commands can be used to set a value to a variable and then substitute that value for the variable later:

%set x 5
5
%mess "x is: $x "
x is: 5
%mess {x is: $x }
x is: $x

Here, the “ ” is used to ensure that the next text printed to the message window will appear on the next line. Without it, the text will continue on the same line. Also note that the set command will also return the value as well as set the variable to the value.

In the third example, note that no substitution, variable or backslash, was done inside the brackets, “{...}”.

Note:  Argument grouping is ALWAYS done before substitution, so the number of arguments input into a command is always determined before any variable or command substitutions that may result in multiple return values.

Creating a Simple Script

The following examples demonstrate how to load multiple geometry files of varying names and save them all as one new geometry file, without using the ANSYS ICEM CFD window (GUI) displayed (GUI stands for Graphical User Interface):

Do the following in the ANSYS ICEM CFD window:

Go to File > Replay Scripts > Replay Control, then load the tetin file.

Save the tetin file to a different name.

You should see the following commands printed:

1. ic_load_tetin support/test/tetin1.tin
2. ic_geo_get_body_matlpnt LIVE.0
3. ic_boco_solver
4. ic_boco_clear_icons
5. ic_boco_natran_csystem reset
6. ic_uns_set_modified 0
7. ic_geo_set_modified 0
8. ic_undo_start
9. ic_save_tetin support/test/new_tetin.tin 0 0

You may discard all but the first and last commands since you are not interested in any GUI updates, boco settings, or the undo log. This is what remains:

1. ic_load_tetin support/test/tetin1.tin
9. ic_save_tetin support/test/new_tetin.tin 0 0

The file name paths start from either the working directory as in this case, or from the top level drive. Since the tetin names can be changed, as well as the working directory, you must use variables. It is safe to specify all names from the top level, so set a variable that is the path of the working directory:

set wdir D:/user/support/test
set tetin tetin1.tin
ic_load_tetin $wdir/$tetin
ic_save_tetin $wdir/new_$tetin.tin 0 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote