Home ยป How to set-up command line arguments in Microsoft Visual Studio (VS Studio Code)

How to set-up command line arguments in Microsoft Visual Studio (VS Studio Code)

I thought I would write this up as a quick blog post. I’m really impressed with VS Studio, after years of working in Eclipse based IDE’s it seems fast and smooth. Setting up command line arguments for the code you are working on however seems harder than you would expect. While in Eclipse there is an option to configure runtime arguments on the menu it is more hidden in VS Code.

Configuring command line arguments in VS Studio Code

To set-up your runtime arguments you need to edit your launch.json file which lives within your projects .vscode directory.

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["Demo.xml", "42"]
}
]
}

You need to add the “args” configuration option line. This key takes a list of arguments which will then be available in your code.

Using command line arguments in your Python code

In your code you can then do something like this:

#Check the program has been called with the Excel Spreadsheet filename and Account Number
if len(sys.argv) != 3:
  print(sys.argv)
  print("Usage ,")
  sys.exit(1)
else:
  # argv[0] in Python is always the name of the script.
  inputFile = sys.argv[1]
  inputAccount = sys.argv[2]
  print(sys.argv)

This works when you press F5 to run the code or start a debug session.




You May Also Like…

Find similar blogs in these categories: Python
0 Comments
Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.