Skip to content

Instantly share code, notes, and snippets.

@bitsgalore
Created April 12, 2022 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsgalore/668eed7ae4421468f83c8ab7e38556f1 to your computer and use it in GitHub Desktop.
Save bitsgalore/668eed7ae4421468f83c8ab7e38556f1 to your computer and use it in GitHub Desktop.
Cross-platform CLI file input with wildcards
import sys
import os
import glob
import platform
import codecs
import argparse
# Create parser
parser = argparse.ArgumentParser(
description="Test CLI input with wildcards, multiple platforms")
def parseCommandLine():
"""Parse command line"""
# Add arguments
parser.add_argument('filesIn',
action="store",
type=str,
nargs='+',
help="input file(s) (wildcards allowed)")
# Parse arguments
args = parser.parse_args()
return args
def main():
"""Main command line application"""
global out
global err
out = codecs.getwriter("UTF-8")(sys.stdout.buffer)
err = codecs.getwriter("UTF-8")(sys.stderr.buffer)
# Get input from command line
args = parseCommandLine()
# In Linux this works for wildcard expressions (but in Windows this is only a string!)
if platform.system() == "Windows":
# Windows doesn't natively handle wildcard expansion, so we need to do it ourselves
filesIn = glob.glob(args.filesIn[0])
else:
# In Linux the OS takes care of the wildcard expansion
filesIn = args.filesIn
print(filesIn)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment