A teammate keeps adding flags to a single script until it has 34 options. How do you restructure it?
IntermediateScenarioAutomation scripting & CLI design
Answer
Situation read → One script is doing several jobs; options interact in confusing ways.
What I'd do →
- Group functionality into subcommands (
tool snapshots cleanup,tool ec2 audit) with a click group. - Move shared options (profile, region, verbosity, format) to the group level and pass them via
ctx.obj. - Put rarely changed settings in a config file with env-var overrides.
- Split the code into modules per subcommand, with a thin CLI layer.
Why → Subcommands make the help text navigable and isolate options that only make sense together.
Interview tip → Mention that kubectl and aws are the model: verb/noun subcommands with global flags.
More on Automation scripting & CLI design
Your team has a 300-line Bash script that parses JSON with `jq`, loops over AWS accounts and retries with `sleep`. It breaks every month. Do you rewrite it in Python?BeginnerA colleague's script deletes files older than 30 days from `/var/log/app`. You are asked to review it before it goes into cron. What do you look for?IntermediateYou need a script that ops engineers run from laptops and that CI runs nightly. How do you design its interface?IntermediateA script reads a 15 GB access log to count 5xx errors per endpoint, and the box runs out of memory. How do you fix it?BeginnerYou must run the same Python automation on 40 servers that have Python 3.6, 3.9 and 3.12. What do you do?IntermediateA cron job that runs a Python script fails silently. Nobody noticed for two weeks. How do you make sure that never happens again?Intermediate