@@ -618,9 +618,50 @@ def _format_code_with_args(source, args):
618
618
line_range = args .line_range )
619
619
620
620
621
+ def find_config_file (args ):
622
+ """Find the configuration file the user specified."""
623
+ config_files = ["pyproject.toml" ]
624
+ flargs = {}
625
+
626
+ config_file = args [args .index ('--config' ) + 1 ]
627
+
628
+ if os .path .isfile (config_file ):
629
+ argfile = os .path .basename (config_file )
630
+ for f in config_files :
631
+ if argfile == f :
632
+ flargs = read_configuration_from_file (config_file )
633
+
634
+ return flargs
635
+
636
+
637
+ def read_configuration_from_file (configfile ):
638
+ """Read docformatter options from a configuration file."""
639
+ flargs = {}
640
+ fullpath , ext = os .path .splitext (configfile )
641
+ filename = os .path .basename (fullpath )
642
+
643
+ if ext == ".toml" :
644
+ import tomli
645
+
646
+ if filename == "pyproject" :
647
+ with open (configfile , "rb" ) as f :
648
+ config = tomli .load (f )
649
+ result = config .get ("tool" , {}).get ("docformatter" , None )
650
+ if result is not None :
651
+ flargs = {k : v if isinstance (v , list ) else str (v )
652
+ for k , v in result .items ()}
653
+
654
+ return flargs
655
+
656
+
621
657
def _main (argv , standard_out , standard_error , standard_in ):
622
658
"""Run internal main entry point."""
623
659
import argparse
660
+
661
+ flargs = {}
662
+ if "--config" in argv :
663
+ flargs = find_config_file (argv )
664
+
624
665
parser = argparse .ArgumentParser (description = __doc__ , prog = 'docformatter' )
625
666
changes = parser .add_mutually_exclusive_group ()
626
667
changes .add_argument ('-i' , '--in-place' , action = 'store_true' ,
@@ -630,44 +671,57 @@ def _main(argv, standard_out, standard_error, standard_in):
630
671
help = 'only check and report incorrectly formatted '
631
672
'files' )
632
673
parser .add_argument ('-r' , '--recursive' , action = 'store_true' ,
674
+ default = bool (flargs .get ('recursive' , False )),
633
675
help = 'drill down directories recursively' )
634
676
parser .add_argument ('-e' , '--exclude' , nargs = '*' ,
635
677
help = 'exclude directories and files by names' )
636
- parser .add_argument ('--wrap-summaries' , default = 79 , type = int ,
678
+ parser .add_argument ('--wrap-summaries' ,
679
+ default = int (flargs .get ('wrap-summaries' , 79 )),
680
+ type = int ,
637
681
metavar = 'length' ,
638
682
help = 'wrap long summary lines at this length; '
639
683
'set to 0 to disable wrapping '
640
684
'(default: %(default)s)' )
641
- parser .add_argument ('--wrap-descriptions' , default = 72 , type = int ,
685
+ parser .add_argument ('--wrap-descriptions' ,
686
+ default = int (flargs .get ('wrap-descriptions' , 72 )),
687
+ type = int ,
642
688
metavar = 'length' ,
643
689
help = 'wrap descriptions at this length; '
644
690
'set to 0 to disable wrapping '
645
691
'(default: %(default)s)' )
646
692
parser .add_argument ('--blank' , dest = 'post_description_blank' ,
647
693
action = 'store_true' ,
694
+ default = bool (flargs .get ('blank' , False )),
648
695
help = 'add blank line after description' )
649
696
parser .add_argument ('--pre-summary-newline' ,
650
697
action = 'store_true' ,
698
+ default = bool (flargs .get ('pre-summary-newline' , False )),
651
699
help = 'add a newline before the summary of a '
652
700
'multi-line docstring' )
653
701
parser .add_argument ('--make-summary-multi-line' ,
654
702
action = 'store_true' ,
703
+ default = bool (flargs .get ('make-summary-multi-line' ,
704
+ False )),
655
705
help = 'add a newline before and after the summary of a '
656
706
'one-line docstring' )
657
707
parser .add_argument ('--force-wrap' , action = 'store_true' ,
708
+ default = bool (flargs .get ('force-wrap' , False )),
658
709
help = 'force descriptions to be wrapped even if it may '
659
710
'result in a mess' )
660
711
parser .add_argument ('--range' , metavar = 'line' , dest = 'line_range' ,
661
- default = None , type = int , nargs = 2 ,
712
+ default = flargs . get ( 'range' , None ) , type = int , nargs = 2 ,
662
713
help = 'apply docformatter to docstrings between these '
663
714
'lines; line numbers are indexed at 1' )
664
715
parser .add_argument ('--docstring-length' , metavar = 'length' ,
665
716
dest = 'length_range' ,
666
- default = None , type = int , nargs = 2 ,
717
+ default = flargs .get ('docstring-length' , None ),
718
+ type = int , nargs = 2 ,
667
719
help = 'apply docformatter to docstrings of given '
668
720
'length range' )
669
721
parser .add_argument ('--version' , action = 'version' ,
670
722
version = '%(prog)s ' + __version__ )
723
+ parser .add_argument ('--config' ,
724
+ help = 'path to file containing docformatter options' )
671
725
parser .add_argument ('files' , nargs = '+' ,
672
726
help = "files to format or '-' for standard in" )
673
727
0 commit comments