Click here to Skip to main content
12,999,744 members (40,955 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 16 Oct 2013

C or C++ Console Program Code Generator

, 19 Oct 2013
Rate this:
Please Sign up or sign in to vote.
The following program is a code generator that generates a C program or a C++ program that has auto-generated parsing code.


    This program generates C or C++ programs that parse command-line arguments and displays the parsed values.  The purpose of this program is to save typing and allowing the rapid creation of programs that can be modified for some intended purpose.

    The program is intended to save typing when writing a C or C++ console program. While the program produces code that runs, it is expected that the user will want to modify the generated code.

   A folder is created with the same name as the base name of the program name passed on the command line and the generated code is created in this folder.


The program was written in Python version 2.7, but it should also run with Python 2.6. The program is made up of the following files:

  • - The main program file that parses the input arguments and calls other python files to create the generated files.
  • - The main code generator, which creates the C or C++ code main file that parses the arguments and displays them.
  • - Stores type, name, and default value information for the generated program arguments.
  • - Stores whether to generate a C or C++ program and other program attributes. 
  • - Identifies if a name is a C++ keyword. Even if C code is being generated, the latest C++ 11 keywords are prohibited.
  • - Creates a Linux Makefile.
  • - Creates file get_arguments.c and get_arguments.h, which are used to parse the C or C++ arguments.
  • - Creates header file platform_os.h, which is used to define platform and operating specific symbols.
  •  - Creates Visual Studio build files.
  • example_program.bat - This is not part of the program. This Windows TM batch file runs the program to produce a sample program. The user must supply a file name after the batch file name. 

I have been compiling recently on Windows using Visual Studio. Linux used to work, and while I expect no issues, it is possible that file platform_os.h and the Makefile might need some tweaking for Linux. I will update the code if there are any issues.

Using the code

How to use this program:

python <program name> <parameter text>

If the program name has the file extension '.cc' or '.cpp' then a C++ program is created. If no file extension is specified or the extension is other than those two C++ file extensions, then a C program is created.

See further below for example command lines.

    Command line format:

        python <program name> <parameter text>

    The formal specification for the program command line is in the form of:

      Below <x> indicates x is required and [y] indicates that y is optional.

        python <program_name> [parameter 1] [parameter 2} ... [parameter N]

    Each parameter is a comma delimited list of the form:


    The parameter_type, parameter_count_token, and any parameter_switch specifier
    can be in any order.

            About variable_name

The variable name must always be the first item specified for each parameter.  The variable name may only contain letters of the English alphabet or the underscore character.  The initial_value for a variable name should only be surrounded by double quote characters if the parameter type is a string, and even then the double quotes are only necessary if the inital_value string contains whitespace.
The only valid initial values for a Boolean parameter are False and True.
If a positional parameter has an initial specified on the command line, then that positional argument is not a required parameter and all subsequent positional arguments are also not required arguments.

            About parameter_type

The parameter_type specifier can be one of the following characters.  If no initial_value is specified for each of these types, the indicated initital value default is used.

        s - A string argument
        i - An integer argument
        f - A floating point argument
        d - A double-precision floating point argument
        b - A Boolean argument

If the parameter_type is not specified, then the parameter_type defaults to 's', which indicates a string argument.

            About parameter_switch

An initial dash character indicate a parameter_switch.  A single dash character indicates a short, or single character, switch name.  Two initial dash characters specify a long-name switch.

Both a short-name switch and a long-name switch can be specified.

The '-h' and '--help' switches are implemented automatically and should not be specified as switch parameters.

            Additional information regarding Boolean parameters.

A Boolean parameter, with parameter_type 'b', is typically used as an optional switch parameter.  Using the switch for a Boolean parameter in the generated program results in the variable name for the Boolean argument being set to the opposite of the initial_value, which for the default for a Boolean parameter, changes the variable from False to True.

    Example command lines:

        python foo alpha,i beta,f file_name gamma,-g,--gm,b

This command line generates a program named 'foo.c' that takes an integer parameter named 'alpha', a floating point parameter  named 'beta', then a string parameter named file_name, and finally
an optional parameter named 'gamma' that is a Boolean value that is only 'True' if either the '-g' switch or the '--gm' switch is specified.

        Created files.
'program name'.c               - For C++, the file extension will be either ".cc" or ".cpp".

platform_os.h                     - A header file with platform and operating specific definitions.

get_arguments.h                - The header file for the 'get_arguments' function.
get_arguments.h                - The implementation file for the 'get_arguments' function.

'base program name'.sln    - A Visual Studio 2008 solution file.

'base program name'.vcproj - A Visual Studio 2008 project file.
Makefile                              - A Linux Makefile.

Example  command lines:

python foo.cpp alpha,i beta,f file_name gamma,-g,--gm,b

The previous command line generates a C++ program named 'foo.cpp' that takes an integer parameter named alpha, a floating point parameter named beta, a string parameter named file_name, and finally an optional parameter named gamma that is a Boolean value that is only TRUE if either the '-g' switch or the '--gm' switch are specified.

python foo file_name='foo.txt'
This command line creates a program named 'foo.c'  that takes a single optional argument named file_name.  If no argument is specified, then file_name will be the value 'foo.txt'.

Points of Interest 

This code evolved over time. If I were to write it again, I might write it differently, but the program works.


  • First posting.
  • Updated the help text in the article and in the code.  Uploaded the latest code.
  • Updated the code to fix a bug when making C++ programs.  Fixed other minor bug.
  • Fixed header file generation so the generated code compiles on Linux too.  Fixed an argument parsing bug.


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Software Developer (Senior)
United States United States
I'm an electrical engineer who has spend most of my career writing software. My background includes Digital Signal Processing, Multimedia programming, Robotics, Text-To-Speech, and Storage products. Most of the code that I've written is in C, C++ and Python. I know Object Oriented Design and I'm a proponent of Design Patterns.

My hobbies include writing software for fun, amateur radio, chess, and performing magic, mostly for charities.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Bartlomiej Filipek1-Jan-14 22:53
memberBartlomiej Filipek1-Jan-14 22:53 
GeneralRe: My vote of 4 Pin
Bill_Hallahan2-Jan-14 15:37
memberBill_Hallahan2-Jan-14 15:37 
GeneralRe: My vote of 4 Pin
Bartlomiej Filipek2-Jan-14 21:01
memberBartlomiej Filipek2-Jan-14 21:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 20 Oct 2013
Article Copyright 2013 by Bill_Hallahan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid