Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1049209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:34:46+00:00 2026-05-16T16:34:46+00:00

What is the best practice (interface and implementation) for a command line tool that

  • 0

What is the best practice (interface and implementation) for a command line tool
that processes selected files in a directory tree?

I give an example that comes to my mind, but I am looking for a ‘best practice’:

flipcase foo.txt foo2.txt

could process foo.txt and save the result as foo2.txt.

flipcase -rv *.txt

could process all text files in the current directory.
-r or --recursive will include all subdirectories.
-v will print some infos to stdout while processing.

One problem that I see with this example is, that the *.txt argument is
sometimes expanded by the shell (Unix and Vista), so I can’t apply this pattern
when walking sub directories.
I guess the reason is, that on Unix such tools are comined with a call to find,
but this seems not to be common on Windows. It also makes it hard to print a
summary at the end.

Requirements:

  • MUST run on Unix, Windows XP, Windows 7 and Mac
  • SHOULD follow common conventions on these platforms.
    (Yes, I know. But I am looking for a reasonable compromise.
    For example it’s Ok to use - instead of / on Windows.)
  • SHOULD not rely on a separate find command, like grep does.
  • MUST work for single files, file patterns and patterns in directory
    hierarchies.
  • SHOULD be build with standard Python libs, e.g. OptionParser and os.walk.
  • COULD handle multiple patterns, e.g. *.txt,*.html.

Other questions on design decisions:

  • What should this tool return (status code)?
  • Which ctrl-keys should this tool handle, and in what way?
  • Should stdin be supported instead of a single file? Configurable or
    auto-detect?
  • Should output redirection be supported? Configurable or auto-detect?
    How deal with debug output in this case?
  • Should the pattern be glob syntax, or a regular expression?
  • Is there a common pattern syntax that supports recursion?
    Maybe recursive:*.txt
    In this case the -r option would not be neccesary.
  • What is best practice to create backups of modified files?
    Option -b, or rather have backups by default and add --no-backup option
  • For single files it should be possible to specify a target file name. How?
  • What status info should be printed, and hot configure this?
    Should it be verbose by default and we allow -q for quiet?
    Or always print a little bit and allow -v (or -vv) to boost this or -q to
    shut up completely?

I don’t really expect to get one single right answer, but may be a handful of
thoughts and pointers to good sample projects.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:34:47+00:00Added an answer on May 16, 2026 at 4:34 pm

    In my experience, the best starting point is to build a tool that follows basic Unix principles — namely, to read from standard input and write to standard output. This allows people to use your tool in a flexible way:

    flipcase input.txt > output.txt
    othercommand | flipcase > output.txt
    flipcase | othercommand > ouput.txt
    flipcase input1.txt  input2.txt > output.txt
    

    The next feature might be in-place editing:

    # Modify input files directly.
    flipcase -i input.txt
    
    # Create backup copies before modifying originals.
    flipcase -i --backup-suffix '_BAK' input.txt
    flipcase -i --backup-prefix 'BAK_' input.txt
    
    # Regex for power users.
    flipcase -i --backup-regex 's/foo/bar/' input.txt
    

    In verbose mode, the tool should not write to standard output, because that would conflict with the core principles above. It should write to standard error or a user-defined log file.

    flipcase -v         input.txt > output.txt
    flipcase -v log.txt input.txt > output.txt
    

    After that, you add recursive behavior. The direction is less clear-cut here, but I’ll toss out a few ideas. In the typical recursive case, the program’s arguments are probably directories, and the user would need to supply additional options to define various types of filtering behavior (that is, which types of files to process).

    flipcase -r -i --backup-suffix '_BAK' --filter-glob '*.txt' dir1 dir2
    flipcase -r -i --backup-suffix '_BAK' --filter-glob '*.txt' --filter-glob 'log*.dat' dir
    flipcase -r -i --backup-suffix '_BAK' --filter-regex 'log\w+\.(txt|log)$' dir1 dir2
    
    # Don't do in-place editing. Instead create new files within the structure.
    flipcase -r --newname-suffix '_NEW'              --filter-glob '*.txt' dir1 dir2
    flipcase -r --newname-regex 's/\.txt$/_new.txt/' --filter-glob '*.txt' dir1 dir2
    
    # Create the backups or the new files in a parallel directory
    # structure rather than within the original structure.
    flipcase -r -i --backup-tree 'backup_dir'   --filter-glob '*.txt' dir1 dir2
    flipcase -r -i --new-tree    'newfiles_dir' --filter-glob '*.txt' dir1 dir2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The best practice seems to be to use assert for a condition that should
I assume the following sample gives a best practice that we should follow when
Best practice is to use unique ivs, but what is unique? Is it unique
Short best practice question: If an object A is injected into another object B,
The 'best practice' (as I see it) to atomically create a new file, is
What would be the best practice to keep the user logged in if he
Is there a best practice for making an entity immutable? Users create exercises in
What is the best practice for generating valid XML with PHP from user submitted
What is the best practice for checking in an EditText for any values before
I've a best practice question on CouchDB (actually I'm using TouchDB a CouchDB port

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.