@@ -8,17 +8,19 @@ import os
88from bibtexparser .bparser import BibTexParser , BibDatabase
99from bibtexparser .bwriter import BibTexWriter
1010
11- def main ():
11+ VERSION = "v1.0"
12+
13+ def get_parser ():
1214 parser = argparse .ArgumentParser (
1315 description = """Extract bibliography entries from LaTeX sources
1416 using a reference BibTeX file""" ,
1517 epilog = """For reporting bugs, giving suggestions and contributing
1618 to this project, visit https://github.com/CodePurble/bibextract"""
1719 )
18- parser .add_argument ("-b " ,
19- "--bib " ,
20- required = True ,
21- help = "BibTeX file to look for entries in "
20+ parser .add_argument ("-v " ,
21+ "--version " ,
22+ help = "Show version information and exit" ,
23+ action = "store_true "
2224 )
2325 parser .add_argument ("-o" ,
2426 "--output" ,
@@ -38,9 +40,10 @@ def main():
3840 help = "Suppress output to stdout" ,
3941 action = "store_true"
4042 )
41- parser .add_argument ("files" ,
43+ parser .add_argument ("-f" ,
44+ "--files" ,
4245 help = """LaTeX files to scan for citations whose BibTeX
43- entries need to be extract """ ,
46+ entries need to be extracted """ ,
4447 action = "extend" ,
4548 nargs = "+"
4649 )
@@ -50,56 +53,78 @@ def main():
5053 file. Default is four spaces""" ,
5154 default = " " ,
5255 )
56+ parser .add_argument ("-d" ,
57+ "--dryrun" ,
58+ help = """Perform a dry-run, i.e. do not output a file,
59+ just print info messages. Ignores the -o option,
60+ affected by -q""" ,
61+ action = "store_true"
62+ )
63+ parser .add_argument ("-b" ,
64+ "--bib" ,
65+ help = "BibTeX file to look for entries in" ,
66+ )
67+ return (parser )
5368
69+ def main ():
70+ parser = get_parser ()
5471 args = parser .parse_args ()
5572
56- # Scan LaTeX files for citations and generate set containing BibTeX entry
57- # labels
58- e_set = set ()
59- for file in args .files :
60- output = subprocess .run ([os .path .dirname (os .path .realpath (__file__ )) + '/ext.sh' ,
61- shlex .quote (file )],
62- capture_output = True
63- )
64- if (output .returncode == 0 ):
65- e_set = e_set .union (set (output .stdout .decode ().rstrip ().split ('\n ' )))
73+ if args .version :
74+ parser .exit (0 , f"{ VERSION } \n " )
75+ else :
76+ if (args .bib is None ) or (args .files is None ):
77+ parser .print_usage ()
78+ parser .exit (2 , "Error: the following arguments are required: -b/--bib, -f/--files\n " )
79+ else :
80+ # Scan LaTeX files for citations and generate set containing BibTeX entry
81+ # labels
82+ e_set = set ()
83+ for file in args .files :
84+ output = subprocess .run ([os .path .dirname (os .path .realpath (__file__ )) + '/ext.sh' ,
85+ shlex .quote (file )],
86+ capture_output = True
87+ )
88+ if (output .returncode == 0 ):
89+ e_set = e_set .union (set (output .stdout .decode ().rstrip ().split ('\n ' )))
6690
67- # Merge custom BibTeX entry labels into main set if any
68- if (args .entries is not None ):
69- with open (args .entries , 'r' ) as en :
70- while (entry := en .readline ()):
71- e_set = e_set .union (entry )
91+ # Merge custom BibTeX entry labels into main set if any
92+ if (args .entries is not None ):
93+ with open (args .entries , 'r' ) as en :
94+ while (entry := en .readline ()):
95+ e_set = e_set .union (entry )
7296
73- parser = BibTexParser ()
74- parser .ignore_nonstandard_types = False
75- with open (args .bib ) as bibtex_file :
76- bib_database = bibtexparser .load (bibtex_file , parser )
97+ parser = BibTexParser ()
98+ parser .ignore_nonstandard_types = False
99+ with open (args .bib ) as bibtex_file :
100+ bib_database = bibtexparser .load (bibtex_file , parser )
77101
78- e_dict = bib_database .entries_dict
79- out_db = BibDatabase ()
102+ e_dict = bib_database .entries_dict
103+ out_db = BibDatabase ()
80104
81- entrycount = 0
82- found = 0
83- for entry in e_set :
84- entrycount += 1
85- if entry in e_dict .keys ():
86- if not args .quiet :
87- print (f"Found: { entry } " )
88- found += 1
89- out_db .entries .append (e_dict [entry ])
90- else :
91- if not args .quiet :
92- print (f"Not found: { entry } " )
105+ entrycount = 0
106+ found = 0
107+ for entry in e_set :
108+ entrycount += 1
109+ if entry in e_dict .keys ():
110+ if not args .quiet :
111+ print (f"Found: { entry } " )
112+ found += 1
113+ out_db .entries .append (e_dict [entry ])
114+ else :
115+ if not args .quiet :
116+ print (f"Not found: { entry } " )
93117
94118
95- writer = BibTexWriter ()
96- writer .indent = args .indent
97- writer .add_trailing_comma = True
98- with open (args .output , 'w' ) as outfile :
99- bibtexparser .dump (out_db , outfile , writer )
119+ writer = BibTexWriter ()
120+ writer .indent = args .indent
121+ writer .add_trailing_comma = True
122+ if not args .dryrun :
123+ with open (args .output , 'w' ) as outfile :
124+ bibtexparser .dump (out_db , outfile , writer )
100125
101- if not args .quiet :
102- print (f"\n Found { found } /{ entrycount } entries" )
126+ if not args .quiet :
127+ print (f"\n Found { found } /{ entrycount } entries" )
103128
104129if __name__ == "__main__" :
105130 main ()
0 commit comments