Table of Contents

Class CalculatorCli

Namespace
CalculatorLibrary
Assembly
CalculatorLibrary.dll

Parses and runs the calculator's command-line arguments.

public static class CalculatorCli
Inheritance
CalculatorCli
Inherited Members

Remarks

The parsing logic lives here, in the library, instead of in CalculatorApp/Program.cs, so it can be unit tested directly (see CalculatorLibrary.Tests/CalculatorCliTests.cs) without spawning the console application as a process. Program.cs only calls Run(string[]) or RunDemo() and prints the result — it never reads from the console, so it never blocks when the app is launched from a script, a build pipeline or CI.

When you rename this sample into your own project, replace this class with your own argument-parsing entry point, keeping the same "parsing is a library function, not something the console shell does inline" shape.

Fields

UsageText

The usage line shown when the arguments cannot be parsed.

public const string UsageText = "Usage: CalculatorApp <add|subtract|multiply|divide> <a> <b>"

Field Value

string

Methods

Run(string[])

Parses args as <operation> <a> <b> and returns a line of text describing the result.

public static string Run(string[] args)

Parameters

args string[]

Exactly three arguments: the operation name (add, subtract, multiply or divide, case-insensitive) followed by the two integer operands.

Returns

string

A human-readable line such as "2 add 2 = 4".

Exceptions

ArgumentException

Thrown when the argument count is wrong, an operand is not a valid 32-bit integer, or the operation name is not recognized. The message always ends with UsageText.

DivideByZeroException

Thrown when dividing by zero.

RunDemo()

Runs a small built-in demonstration that exercises every operation once, used when the application is started with no arguments at all (so a first run always shows something, without waiting on keyboard input).

public static string RunDemo()

Returns

string

The combined demo output, ending with UsageText.